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?
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.
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.
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.
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.
IIRC:
template<class T> template <class U> void Foo<T>::bar() { ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With