Reputation: 1819
this code
std::initializer_list<const char*> list {"something", "somthingElse" /*..*/};
const char* array[] = list;
fails to compile with the following error on error:
array initializer must be an initializer list
Can't really understand what I'm doing wrong here since I'm using an initializer_list
after all.
(The reason I use an initializer_list
is so I can use list.size()
later in my code in several parts; it'd be error_prone having to adjust a series of magic constants each time I add/remove something from the list)
Upvotes: 2
Views: 167
Reputation: 30834
To initialize an array, you need a brace-enclosed initializer list which is not the same as a std::initializer_list
.
To get what you're trying to achieve, you could use a std::array
, but you'll need a helper function to deduce its size parameter:
#include <array>
template<typename T, typename... Ts>
constexpr std::array<T, sizeof...(Ts)> make_array(Ts... i)
{
return {i...};
}
int main() {
auto a = make_array<const char*>( "a", "b", "c", "d" );
return a.size(); // I get an exit value of 4 here
}
Upvotes: 1