473,320 Members | 1,988 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,320 software developers and data experts.

yet another Q: template+friend !!

Here's the code ... just wouldn't compile! (not under g++ at least) I'm
just trying to make test<Y>::mem() a friend of test<X> for all Y and X.
I found quite a lot of very similar things googling around, but not
quite what I needed...not feeling lucky I guess :(

------------------------------Code
start----------------------------------
template <class X> class test
{ X a;
public: test(X b):a(b) {}

friend void fun<>(test); //Compiles fine

template <class Y> void mutatewith(test<Y> b) {a=b.a;}
template <class Y> template <class Z>
friend void test<Y>::mutatewith(test<Z>); //Compiles fine

void mem();
template <class Y> friend void test<Y>::mem(); //ERROR!!
};
template <class X> void fun(test<X> par){par.a=7;}

template class test<double>;

//What if I wanted only the test<Y>::mutatewith(test<X>) to be a friend
of
// test<X> for all Y? Right now, mutatewith has friendship to all
test<Z>
// for all Z !!

-----------------------------code
end----------------------------------------

Samee

Jul 23 '05 #1
6 1105
Samee Zahur wrote:
Here's the code ... just wouldn't compile! (not under g++ at least)
I'm just trying to make test<Y>::mem() a friend of test<X> for all Y
and X. I found quite a lot of very similar things googling around,
but not quite what I needed...not feeling lucky I guess :(

------------------------------Code
start----------------------------------
template <class X> class test
{ X a;
public: test(X b):a(b) {}

friend void fun<>(test); //Compiles fine
Actually doesn't with Comeau C++ (online 'test drive').

template <class Y> void mutatewith(test<Y> b) {a=b.a;}
template <class Y> template <class Z>
friend void test<Y>::mutatewith(test<Z>); //Compiles fine

void mem();
template <class Y> friend void test<Y>::mem(); //ERROR!!
Actually no error with Comeau C++.
};
template <class X> void fun(test<X> par){par.a=7;}

template class test<double>;


I have no suggestion for you, just thought I'd point out the difference
in results for those two compilers.

V
Jul 23 '05 #2
Actually, you don't need to use "template" twice.

template <class Y,class Z>
friend void test<Y>::mutatewith(test<Z>);

The other thing I recommend is to put the function on the next line.
It seems that is the best solution.

template <class Y>
friend void test<Y>::mem();

Your other alternative is to try
friend template <class Y> void test<Y>::mem();

Jul 23 '05 #3
> I have no suggestion for you, just thought I'd point out the
difference
in results for those two compilers.


Actually, when I saw that the online test drive gave errors, I assumed
that they were the same as g++ - felt too lazy to examine what the
errors actually said! Thanks for pointing that out.

In fact, comeau compiles the ENTIRE code just fine ... all I need are
forward declarations of fun!

So, which is standard-compliant in this case? Should it compile and gcc
has a bug? Or should it NOT compile and comeau has an extension/bug ?
Samee

Jul 23 '05 #4
I'm sorry to say this, but NONE of these work (nor are they supposed
to). And wait, since when was introducing line-breaks a language
requirement? C++ treats ALL whitespaces as identical! A space and a
line-break has NO difference whatsoever anywhere in the language!

And I DO need to use "template" twice!

Samee

Jul 23 '05 #5
Samee Zahur wrote:
[..] C++ treats ALL whitespaces as identical! A space and a
line-break has NO difference whatsoever anywhere in the language!


That's simply incorrect. A newline ("line-break") is not allowed
inside a string literal.

V
Jul 23 '05 #6
Ok yes, my mistake thanks. Actually, since I never took a look at the
actual copy of the standard, I can't always come up with the
exceptional cases in my mind. Yes, a newline is not allowed inside a
string literal (but...not sure though...I do remember seeing a case
where a backslash followed by a newline is allowed to represent a
continuous string, but is that legal?)

Anyway, my original problem still stays ... there seems to be no way to
control friendships here. I guess that shouldn't really be a problem
since I am the one writing all versions of the template!

Samee

Jul 23 '05 #7

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

Similar topics

2
by: Christophe Barbe | last post by:
I posted a few days ago about the same problem but was not very clear. So here is my second take at it. Basically with GCC 3.3.2, I can't compile the example from the C++ FAQ Lite available...
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: Ruben Campos | last post by:
Some questions about this code: template <typename T> class MyTemplate; template <typename T> MyTemplate <T> operator- (const MyTemplate <T> & object); template <typename T> MyTemplate <T>...
4
by: frs | last post by:
How can I get a template friend? I want to achieve something like the subsequent two cases: (1) class A { template <typename T> friend class T; }; (2) template <typename T> class A {
2
by: PengYu.UT | last post by:
The following program works with g++3.3. I'm assuming g++-3.4 is more standard conforming that g++-3.3. Would you please tell me what is right way to declare a template friend function of a...
3
by: Pierre Barbier de Reuille | last post by:
Hi, after reading the article " The Standard Librarian : Defining Iterators and Const Iterators" from Matt Austern:...
1
by: curious2007 | last post by:
Hello everybody, When I try to declare nontemplate friend functions I get the following warning: friend decleration......declares a non-template function (if this is not what you intended,...
1
by: farseerfc | last post by:
Hi, everyone here loves C++; As a student studying data structure, I'm writting a single list (with only one pointer to identity the next node in its node struct) simulating std::list like: ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.