Re: Class templates and friend function templates
- From: "John Carson" <jcarson_n_o_sp_am_@xxxxxxxxxxxxxxx>
- Date: Mon, 25 Jul 2005 01:52:53 +1000
"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
.
- Follow-Ups:
- Re: Class templates and friend function templates
- From: Alex Blekhman
- Re: Class templates and friend function templates
- References:
- Class templates and friend function templates
- From: BigMan
- Re: Class templates and friend function templates
- From: Carl Daniel [VC++ MVP]
- Re: Class templates and friend function templates
- From: Carl Daniel [VC++ MVP]
- Re: Class templates and friend function templates
- From: John Carson
- Re: Class templates and friend function templates
- From: Alex Blekhman
- Class templates and friend function templates
- Prev by Date: Re: Class templates and friend function templates
- Next by Date: Re: how can i modify the ip address ,mask,default gateway in VC?
- Previous by thread: Re: Class templates and friend function templates
- Next by thread: Re: Class templates and friend function templates
- Index(es):
Relevant Pages
|
Loading