friend templates

A

Angel Tsankov

Let's say we have a class template:

template< typename t >
class c;

and a function template:

template< typename t1, typename t2 >
bool operator ==
(
c< t1 > const&
, c< t2 > const&
);

Now we want operator ==< t1, t2 > to be a friend of class c< t3 > ONLY for t1 = t3 or t2 = t3. How do we specify this?
 
V

Victor Bazarov

Angel said:
Let's say we have a class template:

template< typename t >
class c;

and a function template:

template< typename t1, typename t2 >
bool operator ==
(
c< t1 > const&
, c< t2 > const&
);

Now we want operator ==< t1, t2 > to be a friend of class c< t3 >
ONLY for t1 = t3 or t2 = t3. How do we specify this?

It is only available if you specialise your 'c' for 't3'. You cannot
declare friendship outside any class definition.

V
 
A

Angel Tsankov

Let's say we have a class template:
It is only available if you specialise your 'c' for 't3'. You cannot
declare friendship outside any class definition.

What if we have access to the definition of c<t >?
 
V

Victor Bazarov

Angel said:
What if we have access to the definition of c<t >?

Fine. If you declare operator==() a friend of the original template,
then it will be a friend of _all_ instantiations of that template.

V
 
A

Angel Tsankov

Victor Bazarov said:
Fine. If you declare operator==() a friend of the original template,
then it will be a friend of _all_ instantiations of that template.

Right, this is what I do not want. I want operator ==<t1,t2> to be a friend of c<t> ONLY for t1 = t3 or t2 = t3.
 
P

prasanna.sethuraman

How can we do that? Can we declare a template as a friend of another
template?
Right, this is what I do not want. I want operator ==<t1,t2> to be a friend of c<t> ONLY for t1 = t3 or t2 = t3.

template <class t>
class SomeTemplateClass {

friend bool operator ==<t, t3>(t&, t3&);
friend bool operator ==<t3, t>(t3&, t&);

// rest of the class declarations
};

Assuming t3 is known, won't the above code ensure that the operator ==
is a friend if and only if one of the arguments is of type t3? No?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,829
Latest member
KimberAlli

Latest Threads

Top