Common template argument in a template function

I have a template class that has a -template function that takes a pointer to the same class as the first argument, for example:

template<class T>
class Foo{
    void f(Foo* foo){}
}

When I use it in my main function, everything works until I use another template for the argument.

int main(){
    Foo<double> f1;
    Foo<double> f2;
    f1.f(&f2); //No errors;

    Foo<bool> f3;
    f1.f(&f3);//Error : No matching function to call to Foo<double>::f(Foo<bool>*&)
}

Apparently the only function defined here is Foo<T>::f(Foo<T>*)

Is there any way to determine fwhich the "generic" template accepts Foo, so that I can use it with any other type?

+4
source share
1 answer

Foo Foo Foo<T>. Foo, f :

template <class T>
class Foo {
    template <class U>
    void f(Foo<U>* foo) { }
};
+11

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


All Articles