Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

templates

Given:

template <class T> class Foo { public:     template <class U>     void bar(); }; 

How do I implement bar outside of the class definition while still having access to both template parameters T and U?

like image 814
May Oakes Avatar asked Feb 24 '11 17:02

May Oakes


People also ask

How do you define a template function outside class?

You can define template methods outside the class definition, in the same header, without using inline and without receiving multiple definition errors. the error no longer occurs.

Can we declare a template function as the friend of the class?

Many-to-one: All instantiations of a template function may be friends to a regular non-template class. One-to-one: A template function instantiated with one set of template arguments may be a friend to one template class instantiated with the same set of template arguments.

How do you define a template function in C++?

Defining a Function TemplateA function template starts with the keyword template followed by template parameter(s) inside <> which is followed by the function definition. In the above code, T is a template argument that accepts different data types ( int , float , etc.), and typename is a keyword.

How do you define a template member function?

You may define a template member function outside of its class template definition. When you call a member function of a class template specialization, the compiler will use the template arguments that you used to generate the class template.


1 Answers

IIRC:

template<class T> template <class U> void Foo<T>::bar() { ... 
like image 136
Anycorn Avatar answered Sep 20 '22 10:09

Anycorn