Initializer list expressions are really convenient for initializing C++ containers:
std::vector<int>({1, 2, 3})
...but it seems that a brace-enclosed initializer list expression, like {1,2,3}
will only bind to a function that takes a std::initializer_list<int>
- it doesn't seem to bind to a universal (forwarding) reference:
template <class T>
void foo(T&& v)
{
std::vector<int>(std::forward<T>(v));
}
int main()
{
foo({1, 2, 3})
}
This outputs:
test2.cpp:11:6: note: template<class U> void foo(U&&)
test2.cpp:11:6: note: template argument deduction/substitution failed:
test2.cpp:33:13: note: couldn't deduce template parameter ‘U’
(This was the result with GCC 4.7.2.)
This unfortunately means we can't forward an initializer list expression. Since it would be very convenient to do that, I'd like to ask why is it that this doesn't work? Why can't a brace enclosed initializer list expression bind to a forwarding reference? Or is this allowed, and perhaps my compiler is just too old?