I like auto in C ++ 11. This is great. But he has one inconsistency that is really nervous because I travel all the time through it:
int i = 3; // i is an int with value 3 int i = int{3}; // i is an int with value 3 int i(3); // i is an int with value 3 (possibly narrowing, not in this case) int i{3}; // i is an int with value 3 auto i = 3; // i is an int with value 3 auto i = int{3}; // i is an int with value 3 auto i(3); // i is an int with value 3 auto i{3}; // wtf, i is a std::initializer_list<int>?!
This strange behavior is confusing for beginners and annoying experienced users - C ++ has quite a few inconsistencies and corner cases that need to be kept in mind as it is. Can someone explain why the standardization committee decided to introduce a new one in this case?
I could understand this if declaring a variable like std::initializer_list was something useful or often executed, but in my experience it was almost never intentional - and in those rare cases when you really wanted to do this, any of
std::initializer_list<int> l{3}; auto l = std::initializer_list<int>{3}; auto l = {3};
will work fine. So what is the reason for the special case for auto x{i} ?
c ++ c ++ 11 initializer-list auto type-deduction
Tristan Brindle Sep 01 '14 at 19:57 2014-09-01 19:57
source share