g++with -std=c++11seems to accept it:
#include <vector>
#include <initializer_list>
std::vector<float> vf={1,2,3};
int main() {}
It would seem that the comment line should be erroneous, but it is not.
Update
Thanks to Jesse for pointing out the standard (8.5.4 p7) which determines why this is normal. Here is an example of code that helps clarify the behavior defined by the standard:
const int v5=5;
int v6=6;
vector<double> vd1={1,2,3,4};
vector<double> vd2={1,2,3,4,v5};
vector<double> vd3={1,2,3,4,v5,v6};
vector<double> vd4={1,2,3,4,v5,static_cast<const int>(v6)};
I hope that the examples just presented will help others avoid some narrowing problems when using initialization lists.
If anyone knows why the latter case violates the standard definition, post a comment.
source
share