In the following code
#include <initializer_list>
#include <utility>
template<typename T> void f(T&& x) {}
template<typename T> void g(std::initializer_list<T> x) {}
int main()
{
auto x = {0}; // OK
auto&& y = {0}; // OK
g(x); // OK
g(std::move(x)); // OK
g({0}); // OK
f(x); // OK
f(std::move(x)); // OK
f({0}); // failure
return 0;
}
rvalue initializer_list
can be deduced with auto
but not with template
.
Why C++ forbids this?
I believe this is due to 14.8.2.1/1:
Now you may think that
auto
is just template argument deduction, but for braced listsauto
receives special treatment in 7.1.6.4/6: