c++ - Variadic template function name lookup fails to find specializations -


i attempting program string concatenation function utilizes 3d library's string conversion functions, implemented variadic template.

the library's conversion function behaves unusually if string (either const char[] literal or std::string) passed it. not possess functions types, want specialize template pull out , not run them through converter. optimization reason if converter handled them.

template<typename t> inline string c(t a) {     return ogre::stringconverter::tostring( ); } template<> inline string c(string s) {     return s; } template<> inline string c(const char s[]) {     return s; }  template<typename t, typename... args> inline string c(t a, args... args) {     return ogre::stringconverter::tostring( ) + c(args...); } template<typename... args> inline string c(string s, args... args) {     return s + c(args...); } template<typename... args> inline string c( const char s[], args... args) {     return s + c(args...); } 

however, when compile program, string literals fall through const char[] specialization , handled base, unspecialized template. command:

u::c( "this dmap[500][500]: ", dmap[500][500], " , 5: ", 5, "." ) 

returns

this dmap[500][500]: 112true5.

"true" being tostring returns if string literal passed it. debugging confirms second string literal caught generic string c(t a, args... args), not first or third, handled specialization.

this seems related problem mentioned in selecting string literal type template specialization, changing template parameter declaration match suggested in solution, inline string c( const char (&s) [n], args... args ), cause first parameter caught specialized template, not second or third. unusual happening here , cannot figure out is.

in

template<typename t, typename... args> inline string c(t a, args... args) {     return ogre::stringconverter::tostring( ) + c(args...); } 

unqualified name lookup c in c(args...) performed in template definition context, means finds overloads of c declared point, , not find later c overloads. (adl performed using both definition , instantiation contexts, in case looks there's no adl.)

declare them first:

template<typename t, typename... args> inline string c(t a, args... args); template<typename... args> inline string c(string s, args... args); template<typename... args> inline string c( const char s[], args... args); 

before define them, 3 overloads can found.


incidentally, should not use specializations single-argument case. delete template<> , use overloads instead. written right now, u::c(""); not behave way want to.

demo.


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -