As C++11 introduces the new uniform initialization syntax, many recommend to use it instead the old style syntax. At least, if it weren't for this so-called corner case:
struct Foo {
Foo(int){
std::cout << "default" << std::endl;
}
Foo(std::initializer_list<int>){
std::cout << "initlist" << std::endl;
}
};
int main(){
Foo f{200}; //prints "initlist"
}
Using a {}-always-style screams for trouble, especially in templates. There seem to be only three safe usages for the new syntax:
- explicitly requesting std::initializer_list-constructors
- POD-constructors
- default-constructors
But there's also a case in which we have to use uniform initialization syntax: non-static data member initializers. For some reason, C++ can recognize
void Bar() {
Foo f(200);
}
but can't deal with
struct Bar {
Foo f(200);
};
Question #1: Why does the ()-syntax work inside a function but not a class? Does anyone know the rationale behind this?
Putting it all together, lastly we arrive at this silly case:
struct FooBar {
std::vector<int> bar(50); //doesn't work
std::vector<int> bar{50}; //not the intended effect
std::vector<int> bar = std::vector<int>(50); //works
};
Of course, you also can't use auto for data members. So I either have to awkwardly mix all syntaxes or not use these features at all.
Question #2: Did I misunderstand something? This can't be intended behavior, can it?