Thread: Templates don't inherit typenames? (GCC specific)

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    1

    Templates don't inherit typenames? (GCC specific)

    I'm porting a program over to gcc, and I get the following error quite a bit:
    foo.cpp:7:2: error: ‘type’ does not name a type
    foo.cpp:7:2: note: (perhaps ‘typename A<xii>::type’ was intended)
    foo.cpp:8:15: error: ‘type’ has not been declared
    with this code:
    Code:
    template <typename foo> class A {
       public:
          typedef int type;
    };
    
    template <typename xii> class B: public A<xii> {
       type ham;
       void cheese (type *) { }
    };
    
    int main (void) {
       return 0;
    }
    I have found that I can workaround the issue by adding this typedef to class B, but it is cumbersome for classes inheriting large amounts of typedefs.
    Code:
    template <typename xii> class B: public A<xii> {
       typedef typename A<xii>::type type;
    
       type ham;
       void cheese (type *) { }
    };
    Anyhow my questions...
    1) Is there a less cumbersome way for me to pull in all of the typenames from the parent template A into B?

    2) Where can I find the actual C++ specifications for this behavior?

    Specifically, this is occuring on gcc version 4.5.1

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    All I can say is that this is standard compliant. That is, gcc is doing the correct thing here.
    Last edited by Elysia; 05-25-2011 at 07:31 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    I can't remember the wording but it's something along the lines of 'If it doesn't depend on the template parameter, base classes aren't used in the name lookup'. The C++ FAQ Lite explains it. It also applies to functions from the base class too which can lead to much fun (read: pain) if you have a nice combination of people who don't know about it and non-compliant compilers

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    The reason why this is so is because templates can be specialized. Just because the template class A contains the typedef, doesn't mean some specialization of A will contain it. It may be missing, or it could have some member variable named 'type'. You need to be explicit about it, which is what you are doing when you say "typedef typename A<xii>::type type"
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. to inherit or not to inherit ...
    By Greenhorn__ in forum C++ Programming
    Replies: 16
    Last Post: 09-01-2008, 11:37 AM
  2. How properly inherit from template?
    By 6tr6tr in forum C++ Programming
    Replies: 118
    Last Post: 04-25-2008, 04:30 AM
  3. Class design (inherit or not)
    By Raigne in forum Game Programming
    Replies: 4
    Last Post: 03-03-2008, 02:10 PM
  4. Friend cannot inherit from private subclass
    By MWAAAHAAA in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2006, 04:44 PM
  5. Replies: 4
    Last Post: 06-28-2003, 11:32 AM