template parameters cannot be friends

S

siddhu

Hello,
would you suggest to me, why gcc 3.3.3 can not compile this:

template<class T>
class Base
{
Base(){}
friend T;
};

class A:virtual public Base<A>
{
public:
A(){}
};

# g++ test.cpp
test.cpp:6: error: template parameters cannot be friends

while CC,MS VisualC++, gcc 2.95 do.
Does it conflict with C++ standard?
How can i change code to produce such behaviour? I need to use
template
Thank you.
 
V

Victor Bazarov

siddhu said:
Hello,
would you suggest to me, why gcc 3.3.3 can not compile this:

template<class T>
class Base
{
Base(){}
friend T;
};

class A:virtual public Base<A>
{
public:
A(){}
};

# g++ test.cpp
test.cpp:6: error: template parameters cannot be friends

while CC,MS VisualC++, gcc 2.95 do.
Does it conflict with C++ standard?
No.

How can i change code to produce such behaviour? I need to use
template

Make the constructor of 'Base' protected.

V
 
S

siddhu

Make the constructor of 'Base' protected.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask- Hide quoted text -

- Show quoted text -

I want class A to be final class. Thats why I have kept Base's
constructor is private. Even if I make it protected ,it is not going
to get compiled.
 
R

red floyd

siddhu said:
[redacted]

I want class A to be final class. Thats why I have kept Base's
constructor is private. Even if I make it protected ,it is not going
to get compiled.

Sorry, I phrased that poorly. This is why you should explain *what* the
problem that you're trying to solve is, rather than *how* you're trying
to solve it.

Once we knew what you were attempting to do, I was able to point you to
the FAQ.
 
S

siddhu

siddhu said:
[redacted]
I want class A to be final class. Thats why I have kept Base's
constructor is private. Even if I make it protected ,it is not going
to get compiled.

Sorry, I phrased that poorly. This is why you should explain *what* the
problem that you're trying to solve is, rather than *how* you're trying
to solve it.

Once we knew what you were attempting to do, I was able to point you to
the FAQ.

I know how to create final class using non-template base class.

This is how i did that:

class Base
{
private:
Base(){}
friend class A;

};

class A:virtual public Base
{
public:
A(){}
};

But I want it to be done using template base class so that it can be
reused to create other final classes.

So I did it in the following way:

template<class T>
class Base
{
Base(){}
friend T;



};


class A:virtual public Base<A>
{
public:
A(){}


};

But it is not getting compiled on gcc 3.3.3.

# g++ test.cpp
test.cpp:6: error: template parameters cannot be friends

while CC,MS VisualC++, gcc 2.95 do.

So how can I achieve this using template base class?
 
V

Victor Bazarov

siddhu said:
siddhu said:
[redacted]
I want class A to be final class. Thats why I have kept Base's
constructor is private. Even if I make it protected ,it is not going
to get compiled.

Sorry, I phrased that poorly. This is why you should explain *what*
the problem that you're trying to solve is, rather than *how* you're
trying to solve it.

Once we knew what you were attempting to do, I was able to point you
to the FAQ.

I know how to create final class using non-template base class.

This is how i did that:

class Base
{
private:
Base(){}
friend class A;

};

class A:virtual public Base
{
public:
A(){}
};

But I want it to be done using template base class so that it can be
reused to create other final classes.

So I did it in the following way:

template<class T>
class Base
{
Base(){}
friend T;

That's a syntax error. You need 'class' there. But don't bother,
it's not going to work.
};


class A:virtual public Base<A>
{
public:
A(){}


};

But it is not getting compiled on gcc 3.3.3.

And it shouldn't compile on any compliant compiler.
# g++ test.cpp
test.cpp:6: error: template parameters cannot be friends

while CC,MS VisualC++, gcc 2.95 do.

All of which are non-compliant in this case.
So how can I achieve this using template base class?

There is no way. You are not allowed to make a template argument
a friend. That's all.

V
 
D

dragoncoder

So how can I achieve this using template base class?
There is no way. You are not allowed to make a template argument
a friend. That's all.
Could you please quote the lines from the standard which enforce this?

Thanks.
 
G

Gianni Mariani

siddhu wrote:
....
So I did it in the following way:

template<class T>
class Base
{
Base(){}
friend T;



};


class A:virtual public Base<A>
{
public:
A(){}


};

But it is not getting compiled on gcc 3.3.3.

# g++ test.cpp
test.cpp:6: error: template parameters cannot be friends

while CC,MS VisualC++, gcc 2.95 do.

So how can I achieve this using template base class?

It compiles in comeau C++ as well.

The gcc 4.3 compiler complains as well.

xxxa.cpp:7: error: a class-key must be used when declaring a friend
xxxa.cpp:7: error: friend declaration does not name a class or function



I tried this... The compilers complained about this ...

template<class T>
class Base
{
Base(){}
friend T::T();
};

class A:virtual public Base<A>
{
public:
A(){}
};

Which begs the question, how do you make a friend constructor ?
 
R

red floyd

dragoncoder said:
Could you please quote the lines from the standard which enforce this?

Actually, I'm interested, too. I couldn't find it in 14.5.3 or in 14.6.5.
 

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
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top