mercredi 7 janvier 2015

pass constexpr intializer_list as argument in c++14

Why doesn't this work:



constexpr initializer_list<int> ilist = {1,2,3,4};
constexpr int my_min = min(ilist);


While this does:



constexpr int my_min = min({1,2,3,4});


I'm basing my code on the constexpr std::min() function as shown here and I'm using clang3.5.0 to compiler (g++4.9.1 doesn't seem to be aware of a constexpr std::min()).


I can't make sense of the error I'm getting:



clang35 -stdlib=libc++ -std=c++14 test.cpp -o test;
test.cpp:158:35: error: constexpr variable 'ilist' must be initialized by a constant expression
constexpr initializer_list<int> ilist = {1,2,3,4};
^ ~~~~~~~~~
test.cpp:158:35: note: pointer to subobject of temporary is not a constant expression
test.cpp:158:43: note: temporary created here
constexpr initializer_list<int> ilist = {1,2,3,4};
^
test.cpp:159:17: error: constexpr variable 'my_min' must be initialized by a constant expression
constexpr int my_min = min(ilist);
^ ~~~~~~~~~~
test.cpp:159:30: note: initializer of 'ilist' is not a constant expression
constexpr int my_min = min(ilist);
^
test.cpp:159:30: note: in call to 'initializer_list(ilist)'
test.cpp:158:35: note: declared here
constexpr initializer_list<int> ilist = {1,2,3,4};

Aucun commentaire:

Enregistrer un commentaire