AUTOSAR C++14 Rule A13-5-2
Description
Rule Definition
All user-defined conversion operators shall be defined explicit.
Rationale
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);
Polyspace Implementation
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.
Troubleshooting
If you expect a rule violation but Polyspace® does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: Overloading |
Category: Required, Automated |
Version History
Introduced in R2020a