When I compile the below code with clang and gcc T
is deduced differently.
#include<initializer_list> //for clang
//to see how T is deduced(form compiler error).
template<typename T>
void foo(T);
int main() {
auto var1{2};
foo(var1);
}
Here is what I got.
clang 3.6(c++11/c++14)
gcc 4.9(c++11/c++14)
T = std::initializer_list<int>
gcc 5.1(c++11/c++14)
T = int
I think T
should be std::initializer_list<int>
.
Why is T = int
in gcc 5.1?
This is proposed change to the C++17 specification - N3922 (I'm not sure if it has been accepted yet).
Basically this presentation from Scott Meyers, slide 20 covers the new rules.
auto var1 {2} ;
Here, var1
will be deduced to be an int
.
It does look like some compilers have already implemented the change. I believe the change is more "intuitive" but your mileage may vary. I think in this interim phase, prefer the =
initialisation, it may be more portable.
The answer here has some more detail on the history of the proposals and defects raised.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With