Inheriting type names from templatized bases - legal or not ?

Discussion in 'C++' started by Jean-Louis Leroy, Mar 5, 2009.

  1. Is the following code legal ?

    template<typename T>
    struct base
    {
    typedef T type;
    };

    template<typename T>
    struct derived : base<T>
    {
    derived(type x);
    };

    g++ 3.3.4 and Visual Studio 2005 accept it. OTOH I don't see how they
    recognize "type" as a type name. I would expect "typename
    base<T>::type" to be required in "derived<T>".

    Jean-Louis Leroy
     
    Jean-Louis Leroy, Mar 5, 2009
    #1
    1. Advertising

  2. * Jean-Louis Leroy:
    > Is the following code legal ?


    Nope.


    > template<typename T>
    > struct base
    > {
    > typedef T type;
    > };
    >
    > template<typename T>
    > struct derived : base<T>
    > {
    > derived(type x);
    > };
    >
    > g++ 3.3.4 and Visual Studio 2005 accept it. OTOH I don't see how they
    > recognize "type" as a type name.


    Comeau C/C++ 4.3.10.1 (Oct 6 2008 11:28:09) for ONLINE_EVALUATION_BETA2
    Copyright 1988-2008 Comeau Computing. All rights reserved.
    MODE:strict errors C++ C++0x_extensions

    "ComeauTest.c", line 10: error: identifier "type" is undefined
    derived(type x);
    ^

    1 error detected in the compilation of "ComeauTest.c".



    > I would expect "typename
    > base<T>::type" to be required in "derived<T>".


    Yep.

    Except for one detail in the context of usage as a base class when deriving.

    As I recall in that context instead of being required, 'typename' is forbidden.


    Cheers & hth.,

    - Alf

    --
    Due to hosting requirements I need visits to [http://alfps.izfree.com/].
    No ads, and there is some C++ stuff! :) Just going there is good. Linking
    to it is even better! Thanks in advance!
     
    Alf P. Steinbach, Mar 5, 2009
    #2
    1. Advertising

  3. > Except for one detail in the context of usage as a base class when deriving.
    >
    > As I recall in that context instead of being required, 'typename' is forbidden.


    Yes, here is an example:

    template<typename T>
    struct foo : /* no typename */ bar<T>::base
    {
    };

    Names appearing in base class lists are necessarily types, so no
    "typename".

    Cheers,
    J-L
     
    Jean-Louis Leroy, Mar 5, 2009
    #3
  4. Jean-Louis Leroy

    SG Guest

    On 5 Mrz., 10:22, Jean-Louis Leroy <> wrote:
    > template<typename T>
    > struct base
    > {
    >   typedef T type;
    >
    > };
    >
    > template<typename T>
    > struct derived : base<T>
    > {
    >   derived(type x);
    >
    > };
    >
    > g++ 3.3.4 and Visual Studio 2005 accept it. OTOH I don't see how they
    > recognize "type" as a type name.


    Maybe they don't recognize it at all. Maybe they are too old and don't
    perform the first phase of the "two phase lookup".

    ( http://womble.decadentplace.org.uk/c /template-faq.html#two-phase )

    > I would expect "typename
    > base<T>::type" to be required in "derived<T>".


    "typename derived::type" also works.

    If you intent to use public or protected base member objects you also
    need to do something about it: using declarations.

    template<typename T>
    struct derived : base<T>
    {
    using base<T>::j;
    //...
    };

    Cheers!
    SG
     
    SG, Mar 5, 2009
    #4
  5. Jean-Louis Leroy

    Bo Persson Guest

    Jean-Louis Leroy wrote:
    > Is the following code legal ?
    >
    > template<typename T>
    > struct base
    > {
    > typedef T type;
    > };
    >
    > template<typename T>
    > struct derived : base<T>
    > {
    > derived(type x);
    > };
    >
    > g++ 3.3.4 and Visual Studio 2005 accept it. OTOH I don't see how
    > they recognize "type" as a type name. I would expect "typename
    > base<T>::type" to be required in "derived<T>".
    >


    It is required.

    VC require it as well, if you disable the default backward
    compatibility mode ("Disable Language Extensions" option).



    Bo Persson
     
    Bo Persson, Mar 5, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mrstephengross
    Replies:
    5
    Views:
    617
    Larry I Smith
    May 18, 2005
  2. Replies:
    3
    Views:
    286
    Niklas Norrthon
    Dec 29, 2005
  3. Old Wolf
    Replies:
    3
    Views:
    268
    James Kanze
    Oct 8, 2007
  4. Replies:
    1
    Views:
    286
    Arnaud Delobelle
    Feb 28, 2008
  5. Daniel Pitts
    Replies:
    3
    Views:
    466
    Vidar Hasfjord
    Jan 31, 2009
Loading...

Share This Page