How to match a template template function in a template class

I have a template class that declares a friend function that itself has template parameters. The code is as follows:

template <class T>
class C;

template <class T, class U>
void func(C<T>& t);

template <class T>
class C
{
    template <class U>
    friend void func<T, U>(C<T>& t);
private:
    template <class U>
    void f()
    {

    }
};

template <class T, class U>
void func(C<T>& t)
{
    t.f<U>();
}

But when I try to call func, I get a compilation error in the line friend:

'func': matching overloaded function not found

How can I make a func<T, U>friend with C<T>?

+4
source share
1 answer

The key question is that the friend you announced does not match the previous announcement you indicated. The first expects two template parameters, but the second (friend) that you defined accepts only one. Once this is resolved, everything works:

template <class T>
class C;

template <class U, class T>
void func(C<T>& t);

template <class T>
class C
{
    template <class U, class TT>
    friend void func(C<TT>& t);
private:
    template <class U>
    void f()
    {

    }
};

template <class U, class T>
void func(C<T>& t)
{
    t.template f<U>();
}

int main() {
    C<int> c;
    func<bool>(c);
}

.

. U T , , T U.

+3

Source: https://habr.com/ru/post/1692535/


All Articles