AutosarC++17_03-A8.5.4ΒΆ

A constructor taking parameter of type std::initializer_list shall only be defined in classes that internally store a collection of objects

Required inputs: IR

Rationale

If an object is initialized using {} braced-initialization, the compiler strongly prefers constructor taking parameter of type std::initializer_list to other constructors. Usage of constructors taking parameter of type std::initializer_list needs to be limited in order to avoid implicit calls to wrongly deduced constructor candidate of a class.

A class that internally stores a collection of objects is the case in which constructors taking parameter of type std::initializer_list are reasonable, allowing readable initialization of a class with a list of its elements.

Example
// $Id: A8-5-4.cpp 271752 2017-03-23 12:07:07Z piotr.tanski $
#include <algorithm>
#include <cstdint>
#include <initializer_list>
#include <vector>
class A {
public:
A() : x(0), y(0) {}
A(std::int32_t first, std::int32_t second) : x(first), y(second) {}
A(std::initializer_list<std::int32_t> list)
: x(0), y(0) // Non-compliant - class A does not store a collection
           // of objects
{
// ...
}

private:
std::int32_t x;
std::int32_t y;
};
class B {
public:
B() : collection() {}
B(std::int32_t size, std::int32_t value) : collection(size, value) {}
B(std::initializer_list<std::int32_t> list)
: collection(list) // Compliant - class B stores a collection of objects
{}

private:
std::vector<std::int32_t> collection;
};
class C {
public:
C() : array{0} {}
C(std::initializer_list<std::int32_t> list)
: array{0} // Compliant - class C stores a collection of objects
{
std::copy(list.begin(), list.end(), array);
}

private:
static constexpr std::int32_t size = 100;
std::int32_t array[size];
};
class D : public C {
public:
D() : C() {}
D(std::initializer_list<std::int32_t> list)
: C{list} // Compliant - class D inherits a collection of objects
        // from class C
{}
};
class E {
public:
E() : container() {}
E(std::initializer_list<std::int32_t> list)
: container{list} // Compliant - class E stores class C which
                // stores a collection of objects
{}

private:
C container;
};
void f1() noexcept {
A a1{};     // Calls A::A()
A a2{{}};   // Calls A::A(std::initializer_list<std::int32_t>)
A a3{0, 1}; // Calls A::A(std::initializer_list<std::int32_t>)
A a4(0, 1); // Calls A::A(std::int32_t, std::int32_t)
}
void f2() noexcept {
B b1{};      // Calls B::B()
B b2{{}};    // Calls B::B(std::initializer_list<std::int32_t>)
B b3{1, 2};  // Calls B::B(std::initializer_list<std::int32_t>)
B b4(10, 0); // Calls B::B(std::int32_t, std::int32_t)
}
void f3() noexcept {
C c1{};        // Calls C::C()
C c2{{}};      // Calls C::C(std::initializer_list<std::int32_t>)
C c3{1, 2, 3}; // Calls C::C(std::initializer_list<std::int32_t>)
}
void f4() noexcept {
D d1{};        // Calls D::D()
D d2{{}};      // Calls D::D(std::initializer_list<std::int32_t>)
D d3{1, 2, 3}; // Calls D::D(std::initializer_list<std::int32_t>)
}
void f5() noexcept {
E e1{};        // Calls E::E()
E e2{{}};      // Calls E::E(std::initializer_list<std::int32_t>)
E e3{1, 2, 3}; // Calls E::E(std::initializer_list<std::int32_t>)
}
See Also
  • Effective Modern C++ [12]: Item 7. Distinguish between () and {} when creating objects.

Possible Messages

Key

Text

Severity

Disabled

constructor_init_list_container

The class should be a container if a constructor taking std::initializer_list is defined.

None

False

Options