Why template<T> cannot make friend with T?

J

John Carson

ben said:
template <typename T>
class A
{
friend class T; // error, but why?
};

Because the C++ standard says so.

http://groups-beta.google.com/group...Malte+Starostik&rnum=1&hl=en#918a87917621b61e

Presumably allowing it to compile creates problems in some cases (or makes
the compiler harder to write), though I can't help with the details on this.
It may be noted that for some T the friend declaration would not make sense,
e.g., friend class int; This may (or may not) have some relevance.
 
H

Howard Hinnant

John Carson said:
Because the C++ standard says so.

http://groups-beta.google.com/group/comp.lang.c++/browse_frm/thread/fa2f0778df
c62abb/918a87917621b61e?q=May+a+template+argument+be+friends+of+the+template+c
lass+Malte+Starostik&rnum=1&hl=en#918a87917621b61e

Presumably allowing it to compile creates problems in some cases (or makes
the compiler harder to write), though I can't help with the details on this.
It may be noted that for some T the friend declaration would not make sense,
e.g., friend class int; This may (or may not) have some relevance.

Though there's a good chance that this will change in C++0X:

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1722.pdf

-Howard
 
A

Abecedarian

ben said:
, which will be 10 years away???

.... and then for another 20 year you'll need to be compatible with the
current C++ Standard. Which is Not a Bad Thing, IMO: you can be sure
that C++ always means C++98.

::A::
 

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
474,297
Messages
2,571,529
Members
48,243
Latest member
MarkoTuggl

Latest Threads

Top