Bug 15534 - Fails to inherit types from base class
Summary: Fails to inherit types from base class
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.4.0
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-05-19 03:04 UTC by Ivan Godard
Modified: 2005-07-23 22:49 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
Compiler output (-v -save-temps) (952 bytes, text/plain)
2004-05-19 03:04 UTC, Ivan Godard
Details
Source code (-save-temps) (99.26 KB, text/plain)
2004-05-19 03:06 UTC, Ivan Godard
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Ivan Godard 2004-05-19 03:04:21 UTC
In the source at the point of the first error messages:
    typedef
    typename std::allocator<T>::size_type st1;
    typedef
    typename size_type  st;
    typedef
    size_type   st2;
the first typedef works but the second two do not even though the class is derived from std::allocator<T>. In simple cases, e.g.:class A{
public:
    typedef
    int B;
    };

class C : public A {
public:
    B b;
    };

the derived class can inherit the types of the base class, but not here. Sticking in a "typename" doesn't help.
Comment 1 Ivan Godard 2004-05-19 03:04:59 UTC
Created attachment 6334 [details]
Compiler output (-v -save-temps)
Comment 2 Ivan Godard 2004-05-19 03:06:00 UTC
Created attachment 6335 [details]
Source code (-save-temps)
Comment 3 Andrew Pinski 2004-05-19 03:12:08 UTC
This is equivent to:
template <class T> class A
{
  typedef int t;
};

template <class T> class B: A<T>
{
  typedef t t1;
};

Which is invalid as t is not dependent so it needs to be declare not in the subclasses.
The fix would be to declare t1 as "typedef A<T>::t t1;"

Also one the mainline I get the following note:
pr15534.ii:31253: error: `size_type' does not name a type
pr15534.ii:31253: note: (perhaps `typename std::allocator<_CharT>::size_type' was intended)
Comment 4 Ivan Godard 2004-05-19 09:30:00 UTC
Comment three says the original example was invalid:
"Which is invalid as t is not dependent so it needs to be declare not in the subclasses". So here is a different example where "t" *is* dependent, and it fails too.

template <class T> class A
{
typedef T* t;
};

template <class T> class B: A<T>
{
typedef t t1;
};

or am I not understanding what you mean by dependent?

g++3.4.0 gives:
foo.cc:8: error: `t' does not name a type
foo.cc:8: error: (perhaps `typename A<T>::t' was intended)

Ivan
Comment 5 Andrew Pinski 2004-05-19 11:16:03 UTC
You misunderstand. 

Type-dependent expressions (From 14.6.2.2 P3):

Anid-expressionis type-dependent if it contains: 
—an identifier that was declared with a dependent type,
—a template-id that is dependent,
—a conversion-function-id that specifies a dependent type,
—a nested-name-specifier that contains a class-name that names a dependent type. 
Comment 6 Wolfgang Bangerth 2004-05-19 13:42:32 UTC
In your example, 
------ 
template <class T> class B: A<T> 
{ 
typedef t t1; 
}; 
------ 
you reference the name 't'. Since 't' does not depend on the template 
argument 'T', it is non-dependent and needs to be looked up at the 
time of declaration of the template. However, since name lookup does 
not look into template dependent base classes such as A<T> it doesn't 
find anything. The solution is to make sure that 't' is qualified as 
dependent, for example by writing 'typename A<T>::t'. 
 
This is really no different than all the other name lookup things that 
we believe are well-documented in the non-bugs and changes-in-3.4 sections 
of our web pages. Please review these if you are unsure. 
 
W.