| View previous topic :: View next topic |
| Author |
Message |
Samee Zahur Guest
|
Posted: Thu Apr 28, 2005 6:04 pm Post subject: 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
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Fri Apr 29, 2005 1:27 am Post subject: Re: yet another Q: template+friend !! |
|
|
Samee Zahur wrote:
| Quote: | 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').
| Quote: |
template <class Y> void mutatewith(test<Y> b) {a=b.a;}
template <class Y> template <class Z
friend void test
void mem();
template <class Y> friend void test<Y>::mem(); //ERROR!!
|
Actually no error with Comeau C++.
| Quote: | };
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
|
|
| Back to top |
|
 |
Bushido Hacks Guest
|
Posted: Fri Apr 29, 2005 2:11 am Post subject: Re: yet another Q: template+friend !! |
|
|
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();
|
|
| Back to top |
|
 |
Samee Zahur Guest
|
Posted: Sat Apr 30, 2005 10:22 am Post subject: Re: yet another Q: template+friend !! |
|
|
| Quote: | 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
|
|
| Back to top |
|
 |
Samee Zahur Guest
|
Posted: Sat Apr 30, 2005 10:23 am Post subject: Re: yet another Q: template+friend !! |
|
|
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
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Sat Apr 30, 2005 6:45 pm Post subject: Re: yet another Q: template+friend !! |
|
|
Samee Zahur wrote:
| Quote: | [..] 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
|
|
| Back to top |
|
 |
Samee Zahur Guest
|
Posted: Mon May 02, 2005 3:37 am Post subject: Re: yet another Q: template+friend !! |
|
|
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
|
|
| Back to top |
|
 |
|