I have created a template as follows
template<typename T>
void f(T const& t) { }
I wanted for this to be callable by containers but also by initializer lists. I thought it would be initializer_list<int>, when called as follows.
f({1, 2, 3});
But GCC behaves as if it’s not Standards compliant
m.cpp: In function 'int main()':
m.cpp:6:25: warning: deducing 'const T' as 'const std::initializer_list<int>'
m.cpp:4:6: warning: in call to 'void f(const T&) [with T = std::initializer_list<int>]'
m.cpp:6:25: warning: (you can disable this with -fno-deduce-init-list)
Can anyone explain how I can make this work without warnings? Thanks!
A “thing” like {1,2,3} does not qualify as an expression. It has no type. Therefore, no type deduction is done. But C++0x makes an explicit exception for ‘auto’, so
actually works and decltype(x) will be
initializer_list<int>. But this is a special rule that only applies to auto. I guess they wanted to make loops like thesework since this kind of loop exploits the special rule.
As for solving the problem, you could add an
initializer_list<T>overload as a “wrapper”:I didn’t test this but my current understanding is that it should work.