AutosarC++18_03-A12.1.5¶
Common class initialization for non-constant members shall be done by a delegating constructor
Required inputs: IR
| Level | required |
|---|---|
| Target | implementation |
| Enforcement | partially automated |
Rationale
Common initialization of non-constant members in a delegating constructor prevents from code repetition, accidental differences and maintenance problems.
Example
// $Id: A12-1-5.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
#include <cstdint>
class A {
public:
// Compliant
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;
};
class B {
public:
// Non-compliant
B(std::int32_t x, std::int32_t y) : x(x + 8), y(y) {}
explicit B(std::int32_t x) : x(x + 8), y(0) {}
private:
std::int32_t x;
std::int32_t y;
};
See Also
- HIC++ v4.0 [8]: 12.4.5: Use delegating constructors to reduce code duplication.
- C++ Core Guidelines [10]: C.51: Use delegating constructors to represent common actions for all constructors of a class.
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
delegating_constructor |
Use delegating constructor for common initialization. |
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
allow_copy_move_constructor¶
allow_copy_move_constructor : bool = False
only_report_if_delegate_constructor_exists¶
only_report_if_delegate_constructor_exists : bool = False
only_report_if_more_than_common_field¶
only_report_if_more_than_common_field : int = 1
only_report_if_more_than_constructors¶
only_report_if_more_than_constructors : int = 1