Defining a Single Template for a Member Function within a Class Template with Both Templates Used in Member Function

MAGEPAC

I'm currently learning how templates work in C++. In particular, I'm looking at the single member function templates within class templates. To understand what I mean, code is found below.

// foo.h
template<typename A>
class foo {
    template<typename B>
    void boo(B);
};

// foo.cpp
template<typename A>
void foo<A>::boo(B value) {} // compiler error: 'Unknown' type name B

// or if I try this

template<typename B>
void foo<A>::boo(B value) {} // compiler error: Use of undeclared identifier A

I'm trying to use two typenames, one from the class template, and one from the single file template, for that specific function. But in those two versions above, I get compiler errors. How would I bypass this problem?

songyuanyao

Both the two sets of template parameters are required to define the member template.

(emphasis mine)

If the enclosing class declaration is, in turn, a class template, when a member template is defined outside of the class body, it takes two sets of template parameters: one for the enclosing class, and another one for itself:

E.g.

template<typename A>
template<typename B>
void foo<A>::boo(B value) {} 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Defining a proxy for a member function that takes a member function pointer as a template argument

class member template function passing a member specialisation

Function pointer to class template member

Constexpr member function in class template

Specialization of member function of class template

template class - member function specialization

Template class member function instatitation

Variadic member function of template class

Template member function specialization in a template class

Deducing template member function in class template

C++ Template class member on a template function

class template and member function template with requires

Cannot construct an instance of a template class, within a member template function of that template class

Member function wrapper with a single argument template?

Is this virtual member function template?

Interface with template member function

template or member function selection

'Adding Member Function to only Specialized Template of Class

variable no of arguments for class template's member function

Using incomplete type in a member function of a class template

Accessing a static member function of a nested class template

Disabling a template class member function with SFINAE

Using SFINAE to disable template class member function

how to pass pointer to member function of a template class?

Calling member function of a template class recursively

Undefined reference to member function of template base class

Template parameter accepting pointer to class member function

template type deduction in a member function of a templated class

Class member template function call not being deducted