AutosarC++17_03-A8.5.2¶
Braced-initialization {}, without equals sign, shall be used for variable initialization
Required inputs: IR
| Level | required |
|---|---|
| Target | implementation |
| Enforcement | automated |
Rationale
Braced-initialization using {} braces is simpler and less ambiguous than other forms of initialization. It is also safer, because it does not allow narrowing conversions for numeric values, and it is immune to C++’s most vexing parse.
The use of an equals sign for initialization misleads into thinking that an assignment is taking place, even though it is not. For built-in types like int, the difference is academic, but for user-defined types, it is important to explicitly distinguish initialization from assignment, because different function calls are involved.
Note that most vexing parse is a form of syntactic ambiguity resolution in C++, e.g.
“Class c()” could be interpreted either as a variable definition of class “Class” or a function declaration which returns an object of type “Class”.
Note that in order to avoid grammar ambiguities, it is highly recommended to use only braced-initialization {} within templates.
Exceptions
If a class declares both a constructor taking std::initializer_list argument and a constructor which invocation will be ignored in favor of std::initializer_list constructor, this rule is not violated by calling a constructor using () parentheses.
Example
// $Id: A8-5-2.cpp 271715 2017-03-23 10:13:51Z piotr.tanski $
#include <cstdint>
#include <initializer_list>
void f1() noexcept {
std::int32_t x1 =
7.9; // Non-compliant - x1 becomes 7 without compilation error
// std::int32_t y {7.9}; // Compliant - compilation error, narrowing
std::int8_t x2{50}; // Compliant
std::int8_t x3 = {50}; // Non-compliant - std::int8_t x3 {50} is equivalent
// and more readable
std::int8_t x4 =
1.0; // Non-compliant - implicit conversion from double to std::int8_t
std::int8_t x5 = 300; // Non-compliant - narrowing occurs implicitly
std::int8_t x6(x5); // Non-compliant
}
class A {
public:
A(std::int32_t first, std::int32_t second) : x{first}, y{second} {}
private:
std::int32_t x;
std::int32_t y;
};
struct B {
std::int16_t x;
std::int16_t y;
};
class C {
public:
C(std::int32_t first, std::int32_t second) : x{first}, y{second} {}
C(std::initializer_list<std::int32_t> list) : x{0}, y{0} {}
private:
std::int32_t x;
std::int32_t y;
};
void f2() noexcept {
A a1{1, 5}; // Compliant - calls constructor of class A
A a2 = {1, 5}; // Non-compliant - calls a default constructor of class A
// and not copy constructor or assignment operator.
A a3(1, 5); // Non-compliant
B b1{5, 0}; // Compliant - struct members initialization
C c1{2, 2}; // Compliant - C(std::initializer_list<std::int32_t>)
// constructor is
// called
C c2(2, 2); // Compliant by exception - this is the only way to call
// C(std::int32_t, std::int32_t) constructor
C c3{{}}; // Compliant - C(std::initializer_list<std::int32_t>) constructor
// is
// called with an empty initializer_list
C c4({2, 2}); // Compliant by exception -
// C(std::initializer_list<std::int32_t>)
// constructor is called
};
template <typename T, typename U> void f1(T t, U u) noexcept(false) {
std::int32_t x = 0;
T v1(x); // Non-compliant
T v2{x}; // Compliant - v2 is a variable
// auto y = T(u); // Non-compliant - is it construction or cast?
// Compilation error
};
void f3() noexcept {
f1(0, "abcd"); // Compile-time error, cast from const char* to int
}
See Also
- C++ Core Guidelines [10]: ES.23 Prefer the {} initializer syntax.
- C++ Core Guidelines [10]: T.68: Use {} rather than () within templates to avoid ambiguities.
- Effective Modern C++ [12]: Item 7. Distinguish between () and {} when creating objects.
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
old_style_variable_init |
Braced-initialization {}, without equals sign, shall be used for variable 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_auto¶
allow_auto : bool = False
allow_expressions¶
allow_expressions : set[bauhaus.ir.PIR_Class_Name] = set()