MISRA Discussion Forums
Rule 7.0.2: Why the requirement for 'explicit' conversion operator? - 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: Why the requirement for 'explicit' conversion operator? (/showthread.php?tid=1758)



Rule 7.0.2: Why the requirement for 'explicit' conversion operator? - cgpzs - 27-11-2025

Rule 7.0.2 has exceptions for "a class type having an explicit operator bool."

Question: why does the conversion operator have to be 'explicit' for the exception to apply? Even if the conversion operator is implicit, clearly the class was designed to be convertible to bool. This requirement seems therefore excessive.

I'm thinking for example about std::bitset::operator[] which returns a proxy object convertible to bool. However since the conversion operator is not explicit (as specified in the C++ standard), then even performing an explicit cast to bool from the proxy object violates Rule 7.0.2. 

Note: I am aware that the 'const' version of std::bitset::operator[] should return a 'bool' directly, but some implementations return the proxy object also here, so the conversion is still necessary.


RE: Rule 7.0.2: Why the requirement for 'explicit' conversion operator? - misra cpp - 05-12-2025

We are in the process of publishing an erratum to MISRA C++:2023, in that the word 'explicit' has been removed from 7.0.2

Publication is imminent


RE: Rule 7.0.2: Why the requirement for 'explicit' conversion operator? - cgpzs - 08-12-2025

Great to hear, thank you for the update!


RE: Rule 7.0.2: Why the requirement for 'explicit' conversion operator? - misra cpp - 09-01-2026

Thread now closed - please post any follow-up as a new thread