C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

May a template argument be a friends of the template class?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
BigMan
Guest





PostPosted: Mon Mar 28, 2005 12:24 pm    Post subject: May a template argument be a friends of the template class? Reply with quote



This code does not compile on g++ 3.4.2. I want the template argument
to be a friend of the template class. What syntax should I use to
define such relationship?

template< typename t >
class t2
{
friend t; // error: a class-key must be used when declaring a friend
};

Back to top
Peter MacMillan
Guest





PostPosted: Mon Mar 28, 2005 12:45 pm    Post subject: Re: May a template argument be a friends of the template cla Reply with quote



BigMan wrote:
Quote:
This code does not compile on g++ 3.4.2. I want the template argument
to be a friend of the template class. What syntax should I use to
define such relationship?

template< typename t
class t2
{
friend t; // error: a class-key must be used when declaring a friend
};


The following example compiled for me in gcc 3.4.3 with -Wall -ansi

//---
#include
using std::cout;
using std::endl;

template<typename T>
class X
{
public:
X<typename T>(int value) : blah(value) {}

int getValue() { return blah; }
private:
int blah;
friend typename T;
};

class Y {
public:
void peekInside(X<Y> &x) {
x.blah = 24;
}
};


int main() {
X<Y> x(42);
Y y;

y.peekInside(x);

// should output 24
cout << x.getValue() << endl;
}
//---

--
Peter MacMillan
e-mail/msn: [email]peter (AT) writeopen (DOT) com[/email]
icq: 1-874-927

GCS/IT/L d-(-)>-pu s()Sad-) a- C+++(++++)>$ UL>$ P++ L+ E-(-) W++(+++)>$
N o w++>$ O !M- V PS PE Y+ t++ 5 X R* tv- b++(+) DI D+(++)>$ G e++ h r--
y(--)

Back to top
BigMan
Guest





PostPosted: Mon Mar 28, 2005 4:13 pm    Post subject: Re: May a template argument be a friends of the template cla Reply with quote



Well, your code does not compile in my g++ 3.4.2. The problem is:
error: `T' is neither function nor member function; cannot be declared
friend

Back to top
Victor Bazarov
Guest





PostPosted: Tue Mar 29, 2005 1:53 am    Post subject: Re: May a template argument be a friends of the template cla Reply with quote

BigMan wrote:
Quote:
Well, your code does not compile in my g++ 3.4.2. The problem is:
error: `T' is neither function nor member function; cannot be declared
friend

I don't know about g++ 3.4.2 but Comeau C++ (online trial) refuses to
compile

template<class T> class A {
friend class T;
int a;
};

explaining that 'T' "may not be used in an elaborate type specifier",
although I can't find anything against it in the Standard. [Aside:
it would really be nice if compilers in the cases where they give
diagnostic messages re ill-formed code, supplied at least the clause
of the Standard related to the issue, better if it were the paragraph]

It would be nice if somebody more versed in the Standard explained...

V



Back to top
Malte Starostik
Guest





PostPosted: Tue Mar 29, 2005 2:28 am    Post subject: Re: May a template argument be a friends of the template cla Reply with quote

Victor Bazarov schrieb:
Quote:
I don't know about g++ 3.4.2 but Comeau C++ (online trial) refuses to
compile

template<class T> class A {
friend class T;
int a;
};

explaining that 'T' "may not be used in an elaborate type specifier",
although I can't find anything against it in the Standard. [Aside:
it would really be nice if compilers in the cases where they give
diagnostic messages re ill-formed code, supplied at least the clause
of the Standard related to the issue, better if it were the paragraph]

Full Ack.

Quote:
It would be nice if somebody more versed in the Standard explained...

11.4 Friends, paragraph 2 requires an elaborated-type-specifier for a
friend declaration of a class.

7.1.5.3 Elaborated type specifiers, paragraph 2 explicitly refers to it:
[...] If the identifier resolves to a typedef-name or a template
type-parameter, the elaborated-type-specifier is ill-formed [Note: this
implies that, within a class template with a template type-parameter T,
the declaration
friend class T;
is ill-formed]

Not that I have any idea about the rationale for this...

Cheers,
Malte

Back to top
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
 


Powered by phpBB © 2001, 2006 phpBB Group