Nested class template as friend

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ike Naar

    Nested class template as friend

    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

    --
    mail to ike at iae dot nl
  • Rolf Magnus

    #2
    Re: Nested class template as friend

    Ike Naar wrote:
    [color=blue]
    > 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 ?[/color]

    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.
    [color=blue]
    > 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.[/color]

    Try the other way round:

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


    Comment

    • Ike Naar

      #3
      Re: Nested class template as friend

      : 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

      Rolf Magnus <ramagnus@t-online.de> wrote:
      : 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 ;

      --
      mail to ike at iae dot nl

      Comment

      • Jonathan Turkanis

        #4
        Re: Nested class template as friend


        "Ike Naar" <nospam@nospam. invalid> wrote in message
        news:xBa%b.5621 $ks5.19339@typh oon.bart.nl...[color=blue]
        > Given the following C++ snippet:
        >[/color]
        [color=blue]
        >
        > 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[/color]
        a bug in[color=blue]
        > GNU g++ 3.2 ?[/color]

        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




        Comment

        Working...