Re: Class templates and friend function templates
- From: "John Carson" <jcarson_n_o_sp_am_@xxxxxxxxxxxxxxx>
- Date: Sun, 24 Jul 2005 10:49:29 +1000
"Carl Daniel [VC++ MVP]" <cpdaniel_remove_this_and_nospam@xxxxxxxxxxxxxxx> wrote in message news:envtI99jFHA.3936@xxxxxxxxxxxxxxxxxxxx
Carl Daniel [VC++ MVP] wrote:
template <class U,class V> friend bool operator == <T>(U u, V v);
should be:
template <class U,class V> friend bool operator == <T>(C<U> u, C<V> v);
Oddly, VC++ likes it either way, Comeau likes neither. More than likely it's a VC bug that this code compiles (and appears to behave as desired). -cd
Hi Carl,
I have been playing with this for the past several hours.
Your code can be made to compile on Comeau if you make the friend declaration as follows:
template <class U,class V> friend bool operator ==(C<U> u, C<V> v);
i.e., ditch the <T> (of course, one would normally use references).
I presume that your use of <T> is meant to tie friendship to an "own" instantiation of the class. The following attempts to make such a tie. It compiles on both Comeau and VC++ 8.0 Beta 2, but fails to enforce the tie (see below):
template <class T> class C;
template <class U, class V> bool operator ==(C<U>&,C<V>&);
template <class T>
class C
{
public:
C(int x_) : x(x_) {}private:
int x; template <class Other>
friend bool operator == (C<T>&, C<Other>&);
};template <class U, class V>
bool operator == (C<U>& u, C<V> &v)
{
return u.x == v.x;
}int main()
{
C<int> c1(1);
C<float> c2(2);
bool b = c1 == c2;
return 0;
}The thing that puzzled me is this. When c1 is declared, I expected the friend declaration to resolve to:
template<class Other> friend bool operator == (C<int>&, C<Other>&);
as a friend of C<int>. When c2 is declared, I expected the friend declaration to resolve to:
template<class Other> friend bool operator == (C<float>&, C<Other>&);
as a friend of C<float>.
When c1 == c2 is evaluated, the operator called is:
bool operator == (C<int>&, C<float>&);
From what I have said above, I expected this to be a friend of C<int>
but not of C<float>. Yet it is apparently a friend of both according to both Comeau and VC++.
Further experimentation shows that the tying doesn't actually work at all. If I define a global variable
C<int*> c0(0);
then I can access its private data inside the assignment operator called on c1 and c2 even though c0 is of a different type to both c1 and c2.
It would seem that my expectations concerning what friend declarations resolve to were mistaken. This may be because
template<class Other> friend bool operator == (C<int>&, C<Other>&);
would represent a kind of partial specialisation.
I would be grateful for any insight you might offer.
-- 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]
- Class templates and friend function templates
- Prev by Date: Re: Different C runtime libraries
- Next by Date: Re: cast array to structure
- Previous by thread: Re: Class templates and friend function templates
- Next by thread: Re: Class templates and friend function templates
- Index(es):
Relevant Pages
|