AutosarC++17_10-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

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 [10]: 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