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

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

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

Jul 23 '05 #1
4 2668
BigMan wrote:
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: pe***@writeopen.com
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(--)
Jul 23 '05 #2
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

Jul 23 '05 #3
BigMan wrote:
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
Jul 23 '05 #4
Victor Bazarov schrieb:
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
Jul 23 '05 #5

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

Similar topics

2
by: Elven | last post by:
Hi! I was trying to find the solution to this problem, but I don't think I could quite come up with the correct keywords to find it, since I'm pretty sure it's been asked before. In short,...
12
by: Surya Kiran | last post by:
Hi all, I've written a function template. say template <class T> fn (T var) { ... } Is there any way, from within the function, can we check what type of argument we've passed on to the...
2
by: Jason Heyes | last post by:
The following program does not compile. Apparantly "t" is inaccessible. #include <iostream> using namespace std; template <class T> class Foo { T t; public: Foo(T t_) : t(t_) { }
7
by: Thomas Matthews | last post by:
Hi, I am converting my table and record classes into templates. My issue is the syntax of declaring a friend class within the template. I have searched the C++ FAQ Lite (web), the C++...
2
by: franklini | last post by:
hello people i. can anybody help me, i dont know what is wrong with this class. it has something to do with the me trying to override the input output stream. if i dont override it, it works fine....
6
by: Starx | last post by:
With the following class: template <class numericType> class testClass { public: testClass(numericType val = 0); friend testClass operator +(testClass a, testClass b); private: numericType...
3
by: Chris | last post by:
I am having a very strange problem involving virtual functions in template classes. First of all, here is an extremely simplified structure of the two classes I am having problems with. ...
12
by: siddhu | last post by:
Hello, would you suggest to me, why gcc 3.3.3 can not compile this: template<class T> class Base { Base(){} friend T; };
4
by: StephQ | last post by:
According to: http://www.parashift.com/c++-faq-lite/templates.html#faq-35.4 , if my understanding is correct, in template<typename T> class Foo { friend void func (const Foo<T>& foo); }; ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.