AutosarC++19_03-A12.1.6ΒΆ
Derived classes that do not need further explicit initialization and require all the constructors from the base class shall use inheriting constructors
Required inputs: IR
| Level | required |
|---|---|
| Target | implementation |
| Enforcement | automated |
Rationale
Reimplementing constructors that do not need further initialization is error-prone and may lead to using wrong base class constructor accidentally.
Example
// $Id: A12-1-6.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
#include <cstdint>
class A
{
public:
A(std::int32_t x, std::int32_t y) : x(x + 8), y(y) {}
explicit A(std::int32_t x) : A(x, 0) {}
private:
std::int32_t x;
std::int32_t y;
};
// Non-compliant
class B : public A
{
public:
B(std::int32_t x, std::int32_t y) : A(x, y) {}
explicit B(std::int32_t x) : A(x) {}
};
// Compliant
class C : public A
{
public:
using A::A;
};
See Also
- C++ Core Guidelines [11]: C.52: Use inheriting constructors to import constructors into a derived class that does not need further explicit initialization.
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
use_inheriting |
Use inheriting constructors if possible. |
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.