11-10-2024, 12:33 PM
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:
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)?
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
{
public:
operator const char *() const;
};
void f(C c)
{
if (c); // C is converted twice: C -> const char * -> bool
}
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)?