AutosarC++18_03-A8.5.1

In an initialization list, the order of initialization shall be following: (1) virtual base classes in depth and left to right order of the inheritance graph, (2) direct base classes in left to right order of inheritance list, (3) non-static data members in the order they were declared in the class definition

Required inputs: IR

Rationale

To avoid confusion and possible use of uninitialized data members, it is recommendedthat the initialization list matches the actual initialization order.

Regardless of the order of member initializers in a initialization list, the order ofinitialization is always:

  • Virtual base classes in depth and left to right order of the inheritance graph.
  • Direct non-virtual base classes in left to right order of inheritance list.
  • Non-static member data in order of declaration in the class definition.

Note that “The order of derivation is relevant only to determine the order of defaultinitialization by constructors and cleanup by destructors.” [C++14 Language Standard[3]]

Example
// $Id: A8-5-1.cpp 271696 2017-03-23 09:23:09Z piotr.tanski $
#include <cstdint>
#include <string>
class A {};
class B {};
class C : public virtual B, public A {
public:
  C() : B(), A(), s() {} // Compliant

  // C() : A(), B() { }     // Non-compliant - incorrect order of initialization

private:
  std::string s;
};
class D {};
class E {};
class F : public virtual A, public B, public virtual D, public E {
public:
  F() : A(), D(), B(), E(), number1(0), number2(0U) {} // Compliant
  F(F const &oth)
      : B(), E(), A(), D(), number1(oth.number1), number2(oth.number2) {
  } // Non-compliant - incorrect
    // order of initialization

private:
  std::int32_t number1;
  std::uint8_t number2;
};
See Also
  • HIC++ v4.0 [8]:12.4.4 Write members in an initialization list in the order in which they are declared

Possible Messages

Key

Text

Severity

Disabled

cafe_message

{}

None

False

Options

message_predicate

message_predicate : typing.Callable[[Cafe_Message], bool] | None = None

If provided, a custom predicate to filter relevant messages. Receives the message node and should return True for messages to report.
 

reported_messages

reported_messages : set[int] | None = {1719}

If provided, only messages of these types are reported.
 

reported_severities

reported_severities : set[str] = {'error', 'remark', 'warning'}

List of severities to display.
 

use_error_number

use_error_number : bool = False

Whether the error number from the frontend should be used.
 

use_rule_severity

use_rule_severity : bool = True

Whether the rule's severity or the compiler's severity should be used.