Rule 7.0.2: operator const char *() - 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.7 Standard conversions (https://forum.misra.org.uk/forumdisplay.php?fid=193) +---- Thread: Rule 7.0.2: operator const char *() (/showthread.php?tid=1710) |
Rule 7.0.2: operator const char *() - karos - 11-10-2024 Hello, Rule 7.0.2 has an exception for converting pointer to bool, and requires an explicit operator bool() for classes. But what if a class, let's say a wrapper class for strings, provides an operator const char *? Consider the following example: Code: class C Shall the commented line be considered compliant with this rule (because each of the conversion steps by itself is compliant), or not (because as a whole it is the conversion of a class to bool without using an explicit operator bool)? RE: Rule 7.0.2: operator const char *() - misra cpp - 11-10-2024 The later, it should be treated as non-compliant. In effect you've got an object of type C being converted to bool, even if it does go through extra steps. if (c.operator()) ; or if ( c ); // if C declared operator bool(); would both be compliant. RE: Rule 7.0.2: operator const char *() - karos - 14-10-2024 Alright, thank you very much for the clarification. |