yet another Q: template+friend !!

S

Samee Zahur

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
 
V

Victor Bazarov

Samee said:
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
 
B

Bushido Hacks

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();
 
S

Samee Zahur

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
 
S

Samee Zahur

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
 
V

Victor Bazarov

Samee said:
[..] 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
 
S

Samee Zahur

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
 

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
474,294
Messages
2,571,507
Members
48,193
Latest member
DannyRober

Latest Threads

Top