mardi 3 novembre 2015

c++ friend function of template with two parameters

Let I have such class Foo and its friend function ::f:

template <typename T>
class Foo {
public:
    template <typename U>
    friend inline void f(Foo<T>, Foo<U>) {}     
};

Then I call it like this f(Foo<double>(), Foo<int>()), friend of who will be ::f?

Only Foo<double> or it will be friend of Foo<int> also, I mean ::f become friend for Foo<T> for any T at the time of declaration, or at the time of usage compiler generate ::f for T=double and find out that it is friend of Foo<double> and not friend of Foo<int>?

And if I declare ::f like this:

template <typename T>
class Foo {
public:
    template <typename U1, typename U2>
    friend inline void f(Foo<U1>, Foo<U2>) {}
};

Again friend of who become ::f after call f(Foo<double>(), Foo<int>());

Note: the second variant compiled only by clang, not gcc.

Note it would be great to see answers with reference to standard.

Aucun commentaire:

Enregistrer un commentaire