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
| Level | required |
|---|---|
| Target | implementation |
| Enforcement | automated |
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¶
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
message_predicate¶
message_predicate : typing.Callable[[Cafe_Message], bool] | None = None
True for messages to
report.
reported_messages¶
reported_messages : set[int] | None = {1719}
reported_severities¶
reported_severities : set[str] = {'error', 'remark', 'warning'}
use_error_number¶
use_error_number : bool = False
use_rule_severity¶
use_rule_severity : bool = True