Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a template member function of a template class [duplicate]

Tags:

c++

templates

Possible Duplicate:
How do I define a template function within a template class outside of the class definition?

I'm struggling with the syntax for the case where I have a template member function within a template class:

template <typename T> class Foo {     void Bar(const T * t);     template <typename T2> void Bar(const T2 * t); };  template <typename T> void Foo<T>::Bar(const T * t) {     // ... no problem ... }  template <typename T> void Foo<T>::Bar<typename T2>(const T2 * t) {     // ... this is where I'm tearing my hair out ... } 

The first member function is fine, but the template member function which handles types other than the base type of the template class is where I am having problems. For the above case I get the following errors:

template_problem.cpp:12: error: parse error in template argument list template_problem.cpp:12: error: expected ‘,’ or ‘...’ before ‘*’ token template_problem.cpp:12: error: ISO C++ forbids declaration of ‘T2’ with no type template_problem.cpp:12: error: template-id ‘Bar<<expression error> >’ in declaration of primary template template_problem.cpp:12: error: prototype for ‘void Foo<T>::Bar(int)’ does not match any in class ‘Foo<T>’ template_problem.cpp:4: error: candidates are: template<class T> template<class T2> void Foo::Bar(const T2*) template_problem.cpp:7: error:                 void Foo<T>::Bar(const T*) template_problem.cpp:12: error: template definition of non-template ‘void Foo<T>::Bar(int)’ 

and I've also tried every other syntax variation I can think of for the template version of Bar.

like image 231
Paul R Avatar asked Jul 09 '12 12:07

Paul R


People also ask

How would you define member function of template class?

Member functions of class templates (C++ only)You may define a template member function outside of its class template definition. The overloaded addition operator has been defined outside of class X . The statement a + 'z' is equivalent to a. operator+('z') .

How do you declare a template function?

To instantiate a template function explicitly, follow the template keyword by a declaration (not definition) for the function, with the function identifier followed by the template arguments. template float twice<float>( float original ); Template arguments may be omitted when the compiler can infer them.

Can a member function be a template?

Member functions can be function templates in several contexts. All functions of class templates are generic but are not referred to as member templates or member function templates. If these member functions take their own template arguments, they are considered to be member function templates.

Can one template class inherit another template class?

Inheriting from a template classIt is possible to inherit from a template class. All the usual rules for inheritance and polymorphism apply. If we want the new, derived class to be generic it should also be a template class; and pass its template parameter along to the base class.


2 Answers

template<typename T> template<typename T2> void Foo<T>::Bar(const T2* t)  {      // stop tearing your hair out } 
like image 63
jrok Avatar answered Sep 23 '22 19:09

jrok


template <typename T> template <typename T2>  void Foo<T>::Bar(const T2 * t) {     // ... this is where I'm tearing my hair out ... } 

Ugly isn't it.

like image 21
111111 Avatar answered Sep 23 '22 19:09

111111