473,396 Members | 2,059 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

friend templates

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
5 2061
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
>> 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
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

"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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Alexander Stippler | last post by:
Hi what's wrong about the following code? It does not compile. template <typename T> class A { public: class B; };
21
by: Sebastian Faust | last post by:
Hi, is a construction like the following possible: template<class view_model> class template_clase { protected: template_clase() {} virtual ~template_clase() {}
1
by: Dmitry D | last post by:
Hi all, I'm having problems with declaring a template friend function. It seems like I've done everything as explained in C++ FAQ, but still, I'm getting the linker error (unresolved external...
6
by: Ben Ingram | last post by:
Hi all, I am writing a template matrix class in which the template parameters are the number of rows and number of columns. There are a number of reasons why this is an appropriate tradeoff for...
5
by: James Aguilar | last post by:
Hello, all. I saw this in the provided code of a lab that I have due in a couple of weeks in my algorithms class. It compiled fine in g++ but did not compile with the VC++ compiler. It does...
1
by: BigMan | last post by:
I have a class template like this: template< typename t > class c; I'd also like to have an operator == for objects of types, which are different specializations of c: template< typename...
43
by: Zeng | last post by:
It's so messy w/o the "friend" relationship. Does anyone know why it was not supported in C#. It's almost about as bad as it doesn't support the inheritance hierarchy and method reference...
5
by: truce | last post by:
I searched but couldn't find anything _quite_ like this. I have some code I inherited that needs some work. Basically I have a template that is instantiated in the c++ code and so only the...
9
by: Adam Badura | last post by:
I have code like this template<int size> big_int { /* ... */ template<int size2> friend class big_int<size2>; }; What I wanted to achive is to be able to easly convert different sized...
21
by: H9XLrv5oXVNvHiUI | last post by:
Hi, I have a question about injecting friend functions within template classes. My question is specific to gcc (version 3.4.5) used in combination with mingw because this code (or at least code...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.