Inheriting type names from templatized bases - legal or not ?

  • Thread starter Jean-Louis Leroy
  • Start date
J

Jean-Louis Leroy

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
 
A

Alf P. Steinbach

* 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
 
J

Jean-Louis Leroy

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
 
S

SG

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
 
B

Bo Persson

Jean-Louis Leroy said:
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
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top