c++ - Passing initializer as argument -
does initializer have type? if so, what's? in case, how make following code work?
template <typename t> int f(t a) { return 0; } int main() { f({1,2}); }
it give following error:
c.cpp:32:2: error: no matching function call 'f' f({1,2}); ^ c.cpp:18:5: note: candidate template ignored: couldn't infer template argument 't' int f(t a) ^ 1 error generated.
when writing f({1,2})
using list initialization.
if want pass initializer list {1,2}
function can this:
#include <initializer_list> #include <iostream> template <typename t> int f(std::initializer_list<t> a) { for(const t& x : a) { std::cout << x << ", " << std::endl; // element } return 0; } int main() { f({1,2}); }
Comments
Post a Comment