It's possible to initialize c++ vector as below:
vector<int> a(5,0);
vector<int> a{0,0,0,0,0};
But if one write
vector<int> a{5,0};
A vector with 2 elements instead of 5 elements is created, i.e. the constructor of vector taking initializer list as argument is called. But such grammar is also possible:
struct myData {
int k,v;
}
myData mydata{myk, myv};
So my question is, when I write vector<int> a{5,0}
, why the constructor vector(initializer_list L)
is called, instead of vector(int k, int v)
?
My guess is, if a constructor consuming initializer list exists, it will be called, otherwise the initializer list is regarded as argument list. But I'm not sure about this.