473,624 Members | 2,323 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

friends of multiple templates

Hi all,

I have a question about friends functions of a template class.

To make it simple, I would like to do something like this.

Assume that I have a class foo with template T

template<Tclass foo

It is easy to define a friend function bar, depending on two foo<T>'s.
friend foo<Tbar<>(foo< T>,foo<T>);

But how do you define it with a foo<Uand a foo<T>?
friend foo<Tbar<>(foo< U>,foo<T>);
Thanks for the help
Jan 24 '07 #1
9 1692
Klaas Vantournhout wrote:
Hi all,

I have a question about friends functions of a template class.

To make it simple, I would like to do something like this.

Assume that I have a class foo with template T

template<Tclass foo

It is easy to define a friend function bar, depending on two foo<T>'s.
friend foo<Tbar<>(foo< T>,foo<T>);

But how do you define it with a foo<Uand a foo<T>?
friend foo<Tbar<>(foo< U>,foo<T>);
template<class Tclass foo;

template<class T, class U>
foo<Tbar(foo<T> , foo<U>);

template<class Tclass foo
{
int a;
template<class Ufriend foo<Tbar(foo<T> , foo<U>);
};

template<class T, class U>
foo<Tbar(foo<Tf T, foo<UfU)
{
fT.a + fU.a;
return fT;
}

int main() {
foo<intfint;
foo<doublefdoub le;
bar(fint, fdouble);
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jan 24 '07 #2

"Victor Bazarov" <v.********@com Acast.netwrote in message
news:ep******** **@news.datemas .de...
template<class Tclass foo;

template<class T, class U>
foo<Tbar(foo<T> , foo<U>);

template<class Tclass foo
{
int a;
template<class Ufriend foo<Tbar(foo<T> , foo<U>);
};

template<class T, class U>
foo<Tbar(foo<Tf T, foo<UfU)
{
fT.a + fU.a;
return fT;
}

int main() {
foo<intfint;
foo<doublefdoub le;
bar(fint, fdouble);
}
I'm a bit curious why this works (although both comeau and gcc seem to allow
it, however gcc complains with a link error (undefined reference to
`foo<intbar<dou ble>(foo<int>, foo<double>)').

foo<Tdeclares bar<T,Uas a friend, but not bar<U,T>. Therefore,
foo<double>::a shouldn't be accessible in the function bar<int, double(as
that is not a bar<T,Ubut a bar<U,Tto foo<Twith T=double and U=int).
Sadly enough, the standard isn't very clear about these specific cases (or
perhaps I'm misinterpreting something in 14.5.3).

- Sylvester
Jan 26 '07 #3
Sylvester Hesp wrote:
>
"Victor Bazarov" <v.********@com Acast.netwrote in message
news:ep******** **@news.datemas .de...
> template<class Tclass foo;

template<class T, class U>
foo<Tbar(foo<T> , foo<U>);

template<class Tclass foo
{
int a;
template<class Ufriend foo<Tbar(foo<T> , foo<U>);
};

template<class T, class U>
foo<Tbar(foo<Tf T, foo<UfU)
{
fT.a + fU.a;
return fT;
}

int main() {
foo<intfint;
foo<doublefdoub le;
bar(fint, fdouble);
}

I'm a bit curious why this works (although both comeau and gcc seem to
allow it, however gcc complains with a link error (undefined reference to
`foo<intbar<dou ble>(foo<int>, foo<double>)').

foo<Tdeclares bar<T,Uas a friend, but not bar<U,T>. Therefore,
foo<double>::a shouldn't be accessible in the function bar<int, double>
(as that is not a bar<T,Ubut a bar<U,Tto foo<Twith T=double and
U=int). Sadly enough, the standard isn't very clear about these specific
cases (or perhaps I'm misinterpreting something in 14.5.3).

It doesn't work. As a matter of fact, the code above introduces two versions
of bar. The friend version is what is called in main. Since the body of
that function never tries to access private members of foo<U>, the compiler
has no reason to complain. On the other hand, since that function is never
defined, you get a linker error. Let's define it to test this theory:

#include <iostream>

template<class Tclass foo;

template<class T, class U>
foo<Tbar(foo<T> , foo<U>);

template<class Tclass foo
{
int a;
template<class Ufriend foo<Tbar(foo<Ta , foo<Ub) {
std::cout << "template friend called\n";
return ( a );
}
};

template<class T, class U>
foo<Tbar(foo<Tf T, foo<UfU)
{
std::cout << "freestandi ng template called\n";
fT.a + fU.a;
return fT;
}

int main() {
foo<intfint;
foo<doublefdoub le;
bar(fint, fdouble);
}
This prints:

template friend called
However, as soon as the friend tries to access private members of foo<U>,
the code does not compile:

#include <iostream>

template<class Tclass foo;

template<class T, class U>
foo<Tbar(foo<T> , foo<U>);

template<class Tclass foo
{
int a;
template<class Ufriend foo<Tbar(foo<Ta , foo<Ub) {
std::cout << "template friend called\n";
b.a;
return ( a );
}
};

template<class T, class U>
foo<Tbar(foo<Tf T, foo<UfU)
{
std::cout << "freestandi ng template called\n";
fT.a + fU.a;
return fT;
}

int main() {
foo<intfint;
foo<doublefdoub le;
bar(fint, fdouble);
}

As for the original question: I have no idea how to do it without making the
function befriend of unrelated classes, too. But, what about:

#include <iostream>

template<class Tclass foo;

template<class T, class U>
foo<Tbar(foo<Tf T, foo<UfU);

template<class Tclass foo
{
int a;

template< class A, class B >
friend
foo<Abar ( foo<A>, foo<B);

};

template<class T, class U>
foo<Tbar(foo<Tf T, foo<UfU)
{
std::cout << "freestandi ng template called\n";
fT.a + fU.a;
return fT;
}

int main() {
foo<intfint;
foo<doublefdoub le;
bar(fint, fdouble);
}
Best

Kai-Uwe Bux

Jan 26 '07 #4

"Sylvester Hesp" <s.****@oisyn.n lwrote in message
news:45******** *************@n ews.xs4all.nl.. .
>
"Victor Bazarov" <v.********@com Acast.netwrote in message
news:ep******** **@news.datemas .de...
> template<class Tclass foo;

template<class T, class U>
foo<Tbar(foo<T> , foo<U>);

template<class Tclass foo
{
int a;
template<class Ufriend foo<Tbar(foo<T> , foo<U>);
};

template<class T, class U>
foo<Tbar(foo<Tf T, foo<UfU)
{
fT.a + fU.a;
return fT;
}

int main() {
foo<intfint;
foo<doublefdoub le;
bar(fint, fdouble);
}

I'm a bit curious why this works (although both comeau and gcc seem to
allow it, however gcc complains with a link error (undefined reference to
`foo<intbar<dou ble>(foo<int>, foo<double>)').
Actually, after pondering over it for a while, I realized this doesn't do
what you want, and gcc's link error is correct. bar<T,Uis never
instantiated, as the friend declaration in foo<Tdeclares *another*
function bar<U(thus with only 1 template argument), with one of the
parameters fixed to foo<T>. As an actual definition of bar<Uisn't given,
this results in link errors. Since bar<T,Uis never instantiated, it
doesn't produce errors.

Now, if you change the template friend function declaration to a
*definition*, the error actually shows up:

template<class Tclass foo
{
int a;
template<class Ufriend foo<Tbar(foo<Tf T, foo<UfU)
{
fT.a + fU.a;
return fT;
}
};

int main()
{
foo<intfint;
foo<doublefdoub le;
bar(fint, fdouble);
}

Here, both comeau, gcc and VC++ complain about fU.a not being accessible.
This problem don't seem to be solveable at all. When changing foo to

template<class Tclass foo
{
int a;
template<class, class Ufriend foo<Tbar(foo<T> , foo<U>);
template<class U, classfriend foo<Ubar(foo<U> , foo<T>);
};

which would, in my opinion, be the correct friend declarations for our
intents and purposes, both Comeau and GCC complain about both fT.a and fU.a
not being accessible. (VC++ 8.0 still allows it, but I'm not so surprised by
that ;))

- Sylvester
Jan 26 '07 #5

"Kai-Uwe Bux" <jk********@gmx .netwrote in message
news:ep******** **@murdoch.acc. Virginia.EDU...
It doesn't work
Yeah I just realized that, see my other post ;)
Jan 26 '07 #6
Hi Kai and Sylvester,

Thanks for the responses. This is indeed what I was looking for. I
never thought of introducing two different kind of templates for that.

A nice new trick stored in my memory!

But now what if the bar function is an operator like operator+, will
this still work?

regards

Klaas

Kai-Uwe Bux wrote:
Sylvester Hesp wrote:
>"Victor Bazarov" <v.********@com Acast.netwrote in message
news:ep******* ***@news.datema s.de...
>> template<class Tclass foo;

template<class T, class U>
foo<Tbar(foo<T> , foo<U>);

template<class Tclass foo
{
int a;
template<class Ufriend foo<Tbar(foo<T> , foo<U>);
};

template<class T, class U>
foo<Tbar(foo<Tf T, foo<UfU)
{
fT.a + fU.a;
return fT;
}

int main() {
foo<intfint;
foo<doublefdoub le;
bar(fint, fdouble);
}
I'm a bit curious why this works (although both comeau and gcc seem to
allow it, however gcc complains with a link error (undefined reference to
`foo<intbar<do uble>(foo<int>, foo<double>)').

foo<Tdeclare s bar<T,Uas a friend, but not bar<U,T>. Therefore,
foo<double>: :a shouldn't be accessible in the function bar<int, double>
(as that is not a bar<T,Ubut a bar<U,Tto foo<Twith T=double and
U=int). Sadly enough, the standard isn't very clear about these specific
cases (or perhaps I'm misinterpreting something in 14.5.3).


It doesn't work. As a matter of fact, the code above introduces two versions
of bar. The friend version is what is called in main. Since the body of
that function never tries to access private members of foo<U>, the compiler
has no reason to complain. On the other hand, since that function is never
defined, you get a linker error. Let's define it to test this theory:

#include <iostream>

template<class Tclass foo;

template<class T, class U>
foo<Tbar(foo<T> , foo<U>);

template<class Tclass foo
{
int a;
template<class Ufriend foo<Tbar(foo<Ta , foo<Ub) {
std::cout << "template friend called\n";
return ( a );
}
};

template<class T, class U>
foo<Tbar(foo<Tf T, foo<UfU)
{
std::cout << "freestandi ng template called\n";
fT.a + fU.a;
return fT;
}

int main() {
foo<intfint;
foo<doublefdoub le;
bar(fint, fdouble);
}
This prints:

template friend called
However, as soon as the friend tries to access private members of foo<U>,
the code does not compile:

#include <iostream>

template<class Tclass foo;

template<class T, class U>
foo<Tbar(foo<T> , foo<U>);

template<class Tclass foo
{
int a;
template<class Ufriend foo<Tbar(foo<Ta , foo<Ub) {
std::cout << "template friend called\n";
b.a;
return ( a );
}
};

template<class T, class U>
foo<Tbar(foo<Tf T, foo<UfU)
{
std::cout << "freestandi ng template called\n";
fT.a + fU.a;
return fT;
}

int main() {
foo<intfint;
foo<doublefdoub le;
bar(fint, fdouble);
}

As for the original question: I have no idea how to do it without making the
function befriend of unrelated classes, too. But, what about:

#include <iostream>

template<class Tclass foo;

template<class T, class U>
foo<Tbar(foo<Tf T, foo<UfU);

template<class Tclass foo
{
int a;

template< class A, class B >
friend
foo<Abar ( foo<A>, foo<B);

};

template<class T, class U>
foo<Tbar(foo<Tf T, foo<UfU)
{
std::cout << "freestandi ng template called\n";
fT.a + fU.a;
return fT;
}

int main() {
foo<intfint;
foo<doublefdoub le;
bar(fint, fdouble);
}
Best

Kai-Uwe Bux
Jan 31 '07 #7

"Klaas Vantournhout" <no************ @spam.comwrote in message
news:ep******** **@gaudi2.UGent .be...
Hi Kai and Sylvester,

Thanks for the responses. This is indeed what I was looking for. I never
thought of introducing two different kind of templates for that.

A nice new trick stored in my memory!

But now what if the bar function is an operator like operator+, will this
still work?
Sure, operators behave just like regular functions.

Btw, please don't top-post. See 5.4 of the C++ faq lite:
http://www.parashift.com/c++-faq-lit...t.html#faq-5.4

- Sylvester
Jan 31 '07 #8
Klaas Vantournhout wrote [top-posting corrected]
Kai-Uwe Bux wrote:
>Sylvester Hesp wrote:
>>"Victor Bazarov" <v.********@com Acast.netwrote in message
news:ep****** ****@news.datem as.de...
template<class Tclass foo;

template<class T, class U>
foo<Tbar(foo<T> , foo<U>);

template<class Tclass foo
{
int a;
template<class Ufriend foo<Tbar(foo<T> , foo<U>);
};

template<class T, class U>
foo<Tbar(foo<Tf T, foo<UfU)
{
fT.a + fU.a;
return fT;
}

int main() {
foo<intfint;
foo<doublefdoub le;
bar(fint, fdouble);
}

I'm a bit curious why this works (although both comeau and gcc seem to
allow it, however gcc complains with a link error (undefined reference
to `foo<intbar<dou ble>(foo<int>, foo<double>)').

foo<Tdeclar es bar<T,Uas a friend, but not bar<U,T>. Therefore,
foo<double>:: a shouldn't be accessible in the function bar<int, double>
(as that is not a bar<T,Ubut a bar<U,Tto foo<Twith T=double and
U=int). Sadly enough, the standard isn't very clear about these specific
cases (or perhaps I'm misinterpreting something in 14.5.3).


It doesn't work.
[analysis snipped]
>
Hi Kai and Sylvester,

Thanks for the responses. This is indeed what I was looking for. I
never thought of introducing two different kind of templates for that.

A nice new trick stored in my memory!

But now what if the bar function is an operator like operator+, will
this still work?
a) Please don't top post. It is frowned upon around these parts.

b) What about:

#include <iostream>

template<class Tclass foo;

template<class T, class U>
foo<Toperator+ ( foo<TfT, foo<UfU );

template<class Tclass foo {
int a;

template< class A, class B >
friend
foo<Aoperator+ ( foo<A>, foo<B);

};

template<class T, class U>
foo<Toperator+ (foo<TfT, foo<UfU) {
std::cout << "freestandi ng template called\n";
fT.a + fU.a;
return fT;
}

int main() {
foo<intfint;
foo<doublefdoub le;
fint + fdouble;
}
BTW: operator+ somehow suggests a commutative operaton, and it is usually
tricky to guess the result type of operator+ just from the lhs. I would
consider

template < typename LHS, typename RHS >
struct result_of_plus {

typedef some_magic type;

};

template < typename LHS, typename RHS >
typename result_of_plus< LHS,RHS>::type
operator+ ( foo<LHSlhs, foo<RHSrhs ) {
...
}
Best

Kai-Uwe Bux
Jan 31 '07 #9
Sylvester Hesp wrote:
"Klaas Vantournhout" <no************ @spam.comwrote in message
news:ep******** **@gaudi2.UGent .be...
>Hi Kai and Sylvester,

Thanks for the responses. This is indeed what I was looking for. I never
thought of introducing two different kind of templates for that.

A nice new trick stored in my memory!

But now what if the bar function is an operator like operator+, will this
still work?

Sure, operators behave just like regular functions.

Btw, please don't top-post. See 5.4 of the C++ faq lite:
http://www.parashift.com/c++-faq-lit...t.html#faq-5.4

- Sylvester

Okay, forgot this for a while about top-posting.

Thanks again to both of you for replying.

regards
Klaas
Feb 1 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3174
by: Gianni Mariani | last post by:
I have 2 distinct template classes which co-operate, hence are friends. However, I can't seem to figure out what syntax to use to make this work. What is the right(tm) way to write a friend class here ? Here is the code: template <typename A> class Y;
3
7617
by: Michael | last post by:
Hello, I am creating an XSL that is going to create a report from XML we recieve from another system. The XML would look like: <report> <page> <header1> <data1>asdf</data1> <data2>fdas</data2>
7
1329
by: fabio de francesco | last post by:
Hello, I'm just joking with the Subject, but I really don't know how to make a synthesis of two questions about some code I'm trying to write. In the following I post this little code, with "..." meaning what I think can be omitted for brevity. // file database.h
1
1331
by: Tom McCallum | last post by:
Hi, Can someone please tell me the correct syntax (if its possible of course) to specify an output stream operator for a templated class so that I dont need to write the same function for all the derived classes from the template. I have a sample of what it thought it might look like below. ==BEGIN CODE SNIPPET===
11
2588
by: Micha | last post by:
Hello there, I think I've run into some classic c++ pitfall and maybe some of you guys can help me out. For my project I will need to use matrices and vectors and so I decided to implement them by myself. I know there are already tons of vector and matrix implementations, but I wanted to have one taylored for my needs and without debugging someones else code. Also is's become somewhat personal meanwhile ;-).
2
2366
by: Patrick Olurotimi Ige | last post by:
When i run the code below with stored proc :- I get only the first table results :-"templates" even if i fill the dataset with another table for example category,pages etc.. Any ideas? Store procedure below:- ------------------------
2
2613
by: blabla120 | last post by:
Hello XSLT-freaks, I have a source xmlfile that I want to transform to 2 different xmlfiles. source.xml: <books> <book> <title>Die Brandmauer</title>
0
938
by: BP | last post by:
I have a requirement to allow the user to provide multiple templates in the desired order to my program and my program could mail merge the data to the provided templates and create the document with data in order. My idea would be firstly generate one templates from the user provided templates and then the subsequent action should be like the normal mail merge process. Does any body have the samples or idea that how can I merge the...
4
7122
by: sadc1986 | last post by:
How does one introduce multiple worksheets in a excel using Xslt Transforms my code... Please suggest <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel"> <xsl:output method="html" encoding="UTF-8"/>
0
8242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8629
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8341
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8488
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6112
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4183
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1793
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1488
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.