First of all, these are “speculative explanations of the form,” this may be the reason, ”as you call it.
{1,2,3} not only std::initializer_list<int> , but also allows you to initialize types without a constructor. For example:
#include <initializer_list> struct x{ int a,b,c; }; void f(x){ } int main() { f({1,2,3}); }
- The correct code. To show that this is not initializer_list , you can see the following code:
#include <initializer_list> struct x{int a,b,c;}; void f(x){ } int main() { auto il = {1, 2, 3}; f(il); }
Mistake:
prog.cpp: In function 'int main()': prog.cpp:10:9: error: could not convert 'il' from 'std::initializer_list<int>' to 'x'
And now to the question "What is the difference?"
in auto x = {1, 2, 3}; enter the code OK to determine the type, because the encoder explicitly said: "It doesn’t matter which type" using auto
In the case of a function template, he can be sure that he is using a different type. And it’s good to prevent errors in ambiguous cases (this does not look like C ++ style, through).
This will be especially bad if there was 1 function f(x) , and then it was changed to a template one. The programmer wrote to use it as x , and after adding a new function for another type, it changed slightly to call a completely different one.