0

Consider the following piece of code:

struct foo {
  std::vector<int> v;
  foo(std::initializer_list<int> L) : v{L} {}
};

The code above compiles fine and initializes v as expected. Now consider the following piece of code:

struct bar {
  std::array<int, 3> a;
  bar(std::initializer_list<int> L) : a{L} {}
};

The above piece of code gives a compile error.

error: no viable conversion from 'std::initializer_list' to 'int'

Searching the web I found that the "proper" way to initialize a member std::array with a std::list_initializer is to use reinterpret_cast in the following manner:

bar(std::initializer_list<int> L) : a(reinterpret_cast<std::array<int, 3> const&>(*(L.begin()))) {}

Q:

Why I can initialize a member std::vector with an std::initializer_list in the initialization list of a constructor but I can't a member std::array?

Is the work-around showed above with reinterpret_cast the proper way to initialize a member std::array with a std::initializer_list?

4

3 に答える 3

1
foo(std::initializer_list<int> L) : v{L} {}

std::vector has a constructor that accepts a std::initializer_list as input. You are thus initializing the vector itself, not any particular element of the std::vector.

bar(std::initializer_list<int> L) : a{L} {}

std::array does not have a constructor that accepts a std::initializer_list as input (in fact, it does not have any constructors at all, it can only be initialized through aggregate initialization). You are thus trying to initialize a specific element of the array, which is why the compiler complains that it cannot convert a std::initializer_list to an int.

于 2015-06-19T00:17:15.447 に答える