All user-defined conversion operators shall be defined explicit
All user-defined conversion operators shall be defined explicit.
If you do not define a user-defined conversion operator with the
explicit specifier, compilers can perform implicit and often unintended
type conversions from the class type with possibly unexpected results.
The implicit conversion can occur, for instance, when a function accepts a parameter of
a type different from the class type that you pass as argument. For instance, the call to
func here causes an implicit conversion from type
myClass to
int:
class myClass {} {
...
operator int() {...}
};
myClass myClassObject;
void func(int) {...}
func(myClassObject);
The checker flags declarations or in-class definitions of user-defined conversion
operators that do not use the explicit specifier.
For instance, operator int() {} can convert variable of the current
class type to an int variable both implicitly and explicitly but
explicit operator int() {} can only perform explicit
conversions.
If you expect a rule violation but do not see it, refer to the documentation for Polyspace® Bug Finder™ or Polyspace Bug Finder Server™.
| Group: Overloading |
| Category: Required, Automated |