Why is there a difference between template deduction and automatic type for case file std :: initializer_list?

Basically, in C ++ 11 there are three types of type inference:

  • Patterns
  • auto
  • decltype

In most cases, the auto output type and patterns seem to act the same. But there is one case - when we set the auto variable to a value created using the copied {} initializer, we get a compilation error

 no match for call to '(std::initializer_list<int>) (<brace-enclosed initializer list>)' foo({0, 1, 2, 3}); 

Here is a link to a live code sample: http://ideone.com/ODBAZ5

 //foo type would be deduced as std::initializer_list<int> auto foo = {0, 1, 2, 3}; template<typename T> void bar(T t){}; //compiles fine bar( foo ); //next line gives compiler error bar({0, 1, 2, 3}); 

decltype is a completely different story and does not apply to this issue, but auto and templates should do the same (at least what seems reasonable) when type inference, but they obviously are not, and this is confusing. why?

+7
c ++ c ++ 11 templates auto type-deduction
source share

No one has answered this question yet.

See similar questions:

6
Why is there a special type inference rule for automatic and copied initializers in C ++ 11 / C ++ 14?

or similar:

fifteen
argument template crashes / substitution when using std :: function and std :: bind
14
Why are the deductions of the “auto” and “template” types different for simplified initializers?
3
Basic resolution / subtraction of pattern type
3
Partial template argument deduction or workaround for std :: array?
2
Why do you need initializers for all variables when using auto in multiple declarations?
one
How to overwrite a template function to handle type inference
0
Why is "typename" still necessary, even a template type declared as a "class"?
0
deduction of type of function argument (container std, for example, vector) fails when using enable_if and SFINAE
-one
Type inference for values ​​in templates
-one
why is this variable not output as initializer_list in g ++ in c ++ 14?

All Articles