Nested class template as friend

I

Ike Naar

Given the following C++ snippet:

template < typename T > class A { // 1
public : // 2
class AA { } ; // 3
} ; // 4
// 5
template < typename T > class B { // 6
friend class A < T > :: AA ; // 7
} ; // 8

It compiles cleanly with Sun C++, HP C++, Intel ecc 7.1,
GNU g++ 2.95.3 and GNU g++ 2.96 .
However, GNU g++ 3.2 gives the following warning:

a.C:7: warning: `typename A<T>::AA' is implicitly a typename
a.C:7: warning: implicit typename is deprecated, please see the
documentation for details

Is there really anything wrong with the friend declaration, or is it a bug in
GNU g++ 3.2 ?
Replacing the keyword 'class' in line 7 by 'typename' makes
it compile cleanly with GNU g++, but gives compilation
errors with all non-GNU compilers.
Replacing 'class' by 'class typename' is not an option either.

Any thoughts?

Kind regards,
Ike
 
R

Rolf Magnus

Ike said:
Given the following C++ snippet:

template < typename T > class A { // 1
public : // 2
class AA { } ; // 3
} ; // 4
// 5
template < typename T > class B { // 6
friend class A < T > :: AA ; // 7
} ; // 8

It compiles cleanly with Sun C++, HP C++, Intel ecc 7.1,
GNU g++ 2.95.3 and GNU g++ 2.96 .
However, GNU g++ 3.2 gives the following warning:

a.C:7: warning: `typename A<T>::AA' is implicitly a typename
a.C:7: warning: implicit typename is deprecated, please see the
documentation for details

Is there really anything wrong with the friend declaration, or is it a
bug in GNU g++ 3.2 ?

When you need to use a type that depends on a template parameter, you
need to tell the compiler that it's a type by adding the typename
keyword.
Replacing the keyword 'class' in line 7 by 'typename' makes
it compile cleanly with GNU g++, but gives compilation
errors with all non-GNU compilers.
Replacing 'class' by 'class typename' is not an option either.

Try the other way round:

friend typename class A < T > :: AA ; // 7
 
I

Ike Naar

: Ike Naar wrote:
:> template < typename T > class A { // 1
:> public : // 2
:> class AA { } ; // 3
:> } ; // 4
:> // 5
:> template < typename T > class B { // 6
:> friend class A < T > :: AA ; // 7
:> } ; // 8
:>
:> It compiles cleanly with Sun C++, HP C++, Intel ecc 7.1,
:> GNU g++ 2.95.3 and GNU g++ 2.96 but not with GNU g++ 3.2

: When you need to use a type that depends on a template parameter, you
: need to tell the compiler that it's a type by adding the typename
: keyword.

:Ike Naar:
:> Replacing the keyword 'class' in line 7 by 'typename' makes
:> it compile cleanly with GNU g++, but gives compilation
:> errors with all non-GNU compilers.
:> Replacing 'class' by 'class typename' is not an option either.

Rolf Magnus:
: Try the other way round:
: friend typename class A < T > :: AA ; // 7

That's also problematic.

GNU: a.C:7: parse error before `<' token

HP:
Error 19: "a.C", line 7 # Unexpected 'class'.
friend typename class A < T > :: AA ;

Intel: a.C(7): error: expected an identifier
friend typename class A < T > :: AA ;
 
J

Jonathan Turkanis

Ike Naar said:
Given the following C++ snippet:
a.C:7: warning: `typename A<T>::AA' is implicitly a typename
a.C:7: warning: implicit typename is deprecated, please see the
documentation for details

Is there really anything wrong with the friend declaration, or is it a bug in
GNU g++ 3.2 ?

I haven't checked the standard on this point, but I think GCC is wrong
to warn here. Using 'class' tells the compiler to expect a class.
Using typename after class is defintely wrong.

GCC 3.3.1 accepts with no warnings, even using -pedantic. Comeau also
accepts it, so I'd just ignore the warning.

Jonathan
 

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,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top