Basically, in C ++ 11 there are three types of type inference:
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?
c ++ c ++ 11 templates auto type-deduction
mr.pd
source share