AutosarC++18_10-A13.5.3ΒΆ
User-defined conversion operators should not be used
Required inputs: IR
| Level | advisory |
|---|---|
| Target | implementation |
| Enforcement | automated |
Rationale
Explicitly named conversions using dedicated member function eliminate any potential errors that can arise if the type conversion operators have to be used.
If using conversion operators is fundamental in an application domain, see A13-5-2.
Example
// $Id: A13-5-3.cpp 305629 2018-01-29 13:29:25Z piotr.serwa $
#include <iostream>
class Complex
{
public:
Complex (double r, double i = 0.0) : re(r), im(i) {}
explicit operator double() const noexcept { return re; }
double AsDouble() const noexcept { return re; }
private:
double re;
double im;
};
int main(void)
{
Complex c(2.0f);
std::cout << (double) c << std::endl; //compliant with A13-5-2, non-compliant with A13-5-3
std::cout << c.AsDouble() << std::endl; //compliant
return 0;
}
See Also
- JSF December 2005 [8]: AV Rule 177: User-defined conversion functions should be avoided.
- C++ Core Guidelines [11]: C.164: Avoid conversion operators.
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
no_user_defined_conversion |
User-defined conversion operators should not be used. |
None |
False |
Options
This rule shares the following common options: confidence, exclude_in_macros, exclude_messages_in_system_headers, excludes, extend_exclude_to_macro_invocations, includes, justification_checker, languages, post_processing, provider, report_at, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
This rule has no individual options.