Re: Class templates and friend function templates



"Alex Blekhman" <tkfx.N05P4M@xxxxxxxxx> wrote in message
news:ebqBPQEkFHA.3256@xxxxxxxxxxxxxxxxxxxx

The confusion stems from the expectation that function template instantiation is similar to class template instantiation. Actually, they're quite different. Class templates support partial specialization. Partially spesialized type of class participates in type resolution on par with primary template.

Function template doesn't support partial specialization.

I am aware that function templates cannot be partially specialised. However, at no point in the code I supplied is operator== specialised, either fully or partially. The issue is the specialisation of the *friend declaration*, not the specialisation of the operator. Friend declarations have their own rules regarding specialisation.

It would appear in fact that, where friend declarations are concerned,
partial specialisation is ruled out for *both* functions *and* classes. This
is stated by Vandevoorde and Josuttis (C++ Templates: The Complete Guide, p.
117) and experimentation confirms it.

What is a little curious to me is that compilers will accept a friend
declaration for a function that has the appearance of allowing a partial
specialisation of the friend declaration, when in fact the friend
declaration is not specialised at all. When I try to do something analogous
with a class, it won't compile, e.g.,

#include <iostream>
using namespace std;

template<class X, class Y>
class B;

template<class T>
class A
{
    template<class Other>
    friend class B; //won't compile, nor will friend class B<T, Other>;
    int x;
public:
    A(int x_) : x(x_)
    {}
};

template<class X, class Y>
class B
{
public:
    template<class T>
    void foo(A<T>& a)
    {
         cout << a.x;
    }
};


int main() { A<int> a(7); B<double, char> b; b.foo(a); }


-- John Carson


.



Relevant Pages

  • Re: Class templates and friend function templates
    ... friend void process; ... each specialization of the `task' class template has all specializations of the function template `func' as friends. ... It only has one parameter that is distinct, thus implying a kind of partial specialisation of the friend declaration once C is instantiated for a particular type. ...
    (microsoft.public.vc.language)
  • Re: does not compile with "using namespace std"
    ... That means that a specialisation of class Stack for some ... template parameter T will have the following function ... because you haven't provided a definition for the friend operator <<. ...
    (comp.lang.cpp)
  • Re: Determining a variables type
    ... what part of this solution makes use of partial template ... but essentially it boils down to the compiler chooses the most specialised ... This is the part that uses partial specialisation. ...
    (comp.lang.cpp)
  • Re: Class templates and friend function templates
    ... >> Function template doesn't support partial specialization. ... > issue is the specialisation of the *friend declaration*, ... > appearance of allowing a partial specialisation of the ...
    (microsoft.public.vc.language)
  • Re: Implementing a templated "round" function?
    ... At the moment i have two different implementations, ... I did add a second template parameter S for the source type: ... > I could add a specialisation for every combination of S and T, ... template <typename T, typename S> ...
    (comp.lang.cpp)

Loading