CPU-ctor-implicit (C++ only)
Synopsis
Constructors that are callable with a single argument are not declared explicit. Conversion operators shall also be set explicit.
Enabled by default
No
Severity/Certainty
Low/Medium

Full description
Constructors that are callable with a single argument are not declared explicit. This means that nothing prevents the constructor from being used to implicitly convert from a fundamental type to the class type. This check is identical to MISRAC++2008-12-1-3, MISRAC++2023-15.1.3.
Coding standards
- CERT OOP32-CPP
Ensure that single-argument constructors are marked "explicit"
- MISRA C++ 2008 12-1-3
(Required) All constructors that are callable with a single argument of fundamental type shall be declared explicit.
- MISRA C++ 2023 15.1.3
(Required) Conversion operators and constructors that are callable with a single argument shall be explicit
Code examples
The following code example fails the check and will give a warning:
class C{
C(double x){} //should be explicit
};
The following code example passes the check and will not give a warning about this issue:
class C{
explicit C(double x){} //OK
};