Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Inheriting type names from templatized bases - legal or not ?

Reply
Thread Tools

Inheriting type names from templatized bases - legal or not ?

 
 
Jean-Louis Leroy
Guest
Posts: n/a
 
      03-05-2009
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
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      03-05-2009
* 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!
 
Reply With Quote
 
 
 
 
Jean-Louis Leroy
Guest
Posts: n/a
 
      03-05-2009
> 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
 
Reply With Quote
 
SG
Guest
Posts: n/a
 
      03-05-2009
On 5 Mrz., 10:22, Jean-Louis Leroy <(E-Mail Removed)> 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...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
 
Reply With Quote
 
Bo Persson
Guest
Posts: n/a
 
      03-05-2009
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




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
template function with automatically templatized return type. Daniel Pitts C++ 3 01-31-2009 10:09 PM
Odd handling of type.__init__ bases quick@sparq.org Python 1 02-28-2008 11:38 PM
Legal to override same function in different bases? Old Wolf C++ 3 10-08-2007 08:02 AM
templatized copy constructor not being deduced lhr_cool_guy@yahoo.com C++ 3 12-29-2005 08:58 AM
Invoking templatized base class constructor from templatized derived class constructor mrstephengross C++ 5 05-18-2005 07:12 PM



Advertisments