Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rule 7.0.2: Why the requirement for 'explicit' conversion operator?
#1
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.
Reply


Messages In This Thread
Rule 7.0.2: Why the requirement for 'explicit' conversion operator? - by cgpzs - 27-11-2025, 09:04 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)