AUTOSAR C++14 Rule A13-5-3
Description
Rule Definition
User-defined conversion operators should not be used.
Rationale
User-defined conversion operators might be called when you neither want nor expect them
to be called, which can result in unexpected type conversation errors. For instance, in this
code snippet, the user-defined conversion operator converts type
customType
to double
to allow mixed mode
expressions:
class customType { public: customType(int base, int exponent); //.... operator double() const; // Conversion operator, convert customType to double }; customType var1(2,5); double var2 = 0.5 * var1; //Conversion operator called, converts var1 to double
var1
by using
cout << var1;
without defining operator
<<
for customType
objects, the compiler uses the
conversion operator to implicitly convert and print var1
as a
double
. To avoid these unexpected conversions, replace the conversion operator with an
equivalent function. The function must then be called explicitly. If you cannot avoid using
conversion operators in your application, see rule AUTOSAR C++14 Rule
A13-5-2
.
Polyspace Implementation
Polyspace® flags all calls to conversion operators.
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: Advisory, Automated |
Version History
Introduced in R2021a