C++ initializer list inside of a template
c++, c++11, initializer-list, templates
Solution
This works with g++ (Ideone):
#include <vector>
#include <string>
template<typename Tp> std::vector<Tp> makevec (std::initializer_list<Tp> initlist) { return initlist; }
int main() { makevec<std::string> ({"alex", "herrmann"}); }
Which clang++?
Problem
I would like to get to know C++11 a little bit more, So I wrote the simplest possible initializer_list test I knew, but inside of a template, this is a "free" function, and I get this error whenever I compile it (clang++ for pretty formatting) ``` /home/alex/repo/mine/mlcppl/test/utiltest1.cc:16:3: error: no matching function for call to 'makevec' mlcppl::makevec<string> ({"alex", "herrmann"}); ``` Here is the code: ``` namespace mlcppl{ template<typename Tp> vector<Tp> makevec (initializer_list<Tp> initlist) { vector<Tp> vt; for(Tp x : initlist) { vt.insert(vt.end(), x); } return vt; } } ``` and here is where it is called: ``` #include <util.hh> int main() { vector<string> vc; vc = makevec<string> ({"alex", "herrmann"}); return 0; } ``` I have no clue why this would happen, Any suggestions?