By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,991 Members | 1,887 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,991 IT Pros & Developers. It's quick & easy.

friend templates

P: n/a
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?

Apr 13 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Angel Tsankov wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 13 '06 #2

P: n/a
>> 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.


What if we have access to the definition of c<t >?
Apr 13 '06 #3

P: n/a
Angel Tsankov wrote:
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.


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
--
Please remove capital As from my address when replying by mail
Apr 14 '06 #4

P: n/a

"Victor Bazarov" <v.********@comAcast.net> wrote in message news:ke******************************@comcast.com. ..
Angel Tsankov wrote:
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.


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.


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.
Apr 14 '06 #5

P: n/a

Angel Tsankov wrote:
"Victor Bazarov" <v.********@comAcast.net> wrote in message news:ke******************************@comcast.com. ..
Angel Tsankov wrote:
> 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.

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.


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?

Apr 14 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.