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

B

BigMan

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
};
 
P

Peter MacMillan

BigMan said:
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 <iostream>

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: (e-mail address removed)
icq: 1-874-927

GCS/IT/L d-(-)>-pu s():(-) 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(--)
 
B

BigMan

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
 
V

Victor Bazarov

BigMan said:
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
 
M

Malte Starostik

Victor said:
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.
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
 

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

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top