c++ - Defining a Template Member of a Template Class separately from the Declaration -


#include <cstdlib>  template<class a> struct foo {     template<class b> static bool bar(); };  template<class b> template<class a>  bool foo<a>::bar<b>() {     return true; }  int main() {     bool b = foo<int>::bar<long>();     b; } 

this results in linker error:

main.obj : error lnk2019: unresolved external symbol "public: static bool __cdecl foo<int>::bar<long>(void)" (??$bar@j@?$foo@h@@sa_nxz) referenced in function main 

i need define member function outside of class template's declaration. in other words, cannot this:

#include <cstdlib> template<class a> struct foo {     template<class b> static bool bar()     {         return true;     } };  int main() {     bool b = foo<int>::bar<long>();     b; } 

what missing? how can define member function template? what's needed syntax?

note: using msvc 2008, in case that's relevant.

edit

the first thing tried reverse order of template<class a> , template<class b>:

#include <cstdlib>  template<class a> struct foo {     template<class b> static bool bar(); };  template<class a> template<class b>  bool foo<a>::bar<b>() {     return true; }  int main() {     bool b = foo<int>::bar<long>();     b; } 

this resulted in compiler error:

.\main.cpp(11) : error c2768: 'foo<a>::bar' : illegal use of explicit template arguments 

on closing brace of definition bar function.

just reverse order of template<class b> template<class a>. second 1 "inner" , goes member declaration. see §14.5.2/1.

also, john points out, remove parameter-list bar<b>.

// "outer" template: parameter gets substituted create "inner" template template< class >  // "inner" template stands alone after "outer" substitution template< class b >  bool // class name after "outer" substitution.       foo<a> // has usual function template syntax              :: bar() { 

Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -