C ++ initializer inside the template

I would like to learn C ++ 11 a little more, so I wrote the simplest possible initializer_list test that I knew, but inside the template it 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 it is called:

 #include <util.hh> int main() { vector<string> vc; vc = makevec<string> ({"alex", "herrmann"}); return 0; } 

I have no clue why this will happen, Any suggestions?

+4
source share
1 answer

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 ++?

+4
source

Source: https://habr.com/ru/post/1412444/


All Articles