Rule Definition
The explicit keyword prevents the constructor from being used to implicitly convert a fundamental type to the class type.
This can lead to unpredictable behaviour and hard to catch bugs
Remediation
Declare such constructors as explicit
Violation Code Sample
class C
{
public:
C ( int32_t a ) // Non-compliant
{
}
};
Fixed Code Sample
class D
{
public:
explicit D ( int32_t a ) // Compliant
{
}
};
Reference
MISRA C++ 2008, Rule 12-1-3: All constructors that are callable with a single argument of fundamental type shall be declared explicit.
AUTOSAR C++ 2014, Rule M12-1-3: All constructors that are callable with a single argument of fundamental type shall be declared explicit.
Related Technologies
Technical Criterion
Programming Practices - Unexpected Behavior
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.