I've been playing around with C++11 for the past few days, and I came up with something strange.
If I want to uniformly initialize an int:
int a{5};
But if I do the same thing to a std::vector:
std::vector<int> b{2};
Does not construct a two element array, but rather an array with one element of value two. It seems like to get that effect one would need to be more explicit about it:
std::vector<int> c{{2}};
std::vector<int> d = {2};
But not like the declaration of b - this seems inconsistent. I have seen some other stuff to the same effect. What I'm asking - is this behavior in the final C++11 standard, or is it just in a draft that was implemented early? If so, why did the standards committee include this behavior? It seems like it defeats the whole purpose of uniform initialization, as one has to remember which classes have initializer list constructors, and to use the old () syntax instead of {} with just those classes. Or one forgoes uniform initialization altogether.
This seems like a big "gotcha". But there might be advantages to it that I am not aware of.
Edit: this code:
#include <iostream>
#include <vector>
int main() {
std::vector<int> a{2};
for (auto x: a) {
std::cout << x << std::endl;
}
return 0;
}
outputs "2" on gcc 4.6.2