Rule 10.1.2: Justification for considering volatile member functions as inappropriate - Printable Version +- MISRA Discussion Forums (https://forum.misra.org.uk) +-- Forum: MISRA C++ (https://forum.misra.org.uk/forumdisplay.php?fid=18) +--- Forum: MISRA C++:2023 guidelines (https://forum.misra.org.uk/forumdisplay.php?fid=188) +---- Forum: 4.10 Declarations (https://forum.misra.org.uk/forumdisplay.php?fid=196) +---- Thread: Rule 10.1.2: Justification for considering volatile member functions as inappropriate (/showthread.php?tid=1696) |
Rule 10.1.2: Justification for considering volatile member functions as inappropriate - Nicolas Huch - 14-08-2024 I seek justification for why declaring a member function as volatile is considered an inappropriate use of the volatile qualifier. The other inappropriate use cases (local variables, function parameters, function return types, and structured bindings) are justifiable to me since they are not bound to specific memory addresses, which may be out of scope for the C++ virtual machine to reason about. Instead, they are typically automatic variables located on the stack or only exist in registers, which conflicts with the concept of volatile. However, a class object might be located at a specific memory address where it could abstract a memory-mapped register. Such an object must be declared as volatile. To call non-static member functions on such an object, those functions must also be declared as volatile. The justification for all of the inappropriate use cases mentions that the behavior is not well-defined or well-understood, but the standard clearly states the behavior in the case of non-static member functions: Declaring a non-static member function as volatile will influence the type of the this pointer as well as the member function type (12.2.2 [4]). Section 12.2.2.1 [1] further clarifies that the type of the object referred to by the this pointer is qualified with the same cv-qualifiers as those used for the non-static member function. Additionally, MISRA Rule 10.1.2 explicitly states in a note that "a pointer or reference to a volatile entity is permitted", which contradicts the notion that declaring a volatile member function is an inappropriate use of the volatile qualifier. Are there further aspects I am missing or wrong assumptions in my reasoning? Kind regards Nicolas RE: Rule 10.1.2: Justification for considering volatile member functions as inappropriate - misra cpp - 27-09-2024 While we agree that the mechanics of C++ require a member function to be declared `volatile` in order for it to be called on a volatile object, MISRA's intent is to make it clear that it is the member data that is volatile. struct Data { int i; int j; }; struct UseData { int getData() const { return m_data->i; } Data volatile * m_data; }; |