template inherits from template passing its argument is not legal c++ ?

N

nutty

Dear group,

I tested this code in a 4.x gcc, MSVC 8.0, 7.1 and comeau in strict and
relaxed mode.


It compiles in MSVC and relaxed comeau, but fails in gcc 4.x and strict
comeau

It seems it is not possible to derive from a template class and pass it
an argument that is a template parameter itself in the derived class.
This would break a lot of my code.
But I could imagine that this is just not the exactly right way to code
it. Does anyone have any hints on this?
Does anyone know, wether it is possible to tell the gcc to compile in
something similar to the comeau relaxed mode?
And finally, can anyone tell, what the standard says? I don't have the
standard unfortunately.

Thanks for your help
Ingo

// main.cpp

template< typename dataT >
struct A
{
typedef dataT DataT;

DataT* _pCol;
int _n;
};

template< typename T >
struct B : A< int >
{
typedef DataT CanISeeIt;

void func( )
{
_pCol;
_n;
}

};

template< typename T >
struct C : A< T >
{
typedef DataT CanISeeIt;

void func( )
{
_pCol;
_n;
}
};

int main( ){}
 
M

mlimber

nutty said:
Dear group,

I tested this code in a 4.x gcc, MSVC 8.0, 7.1 and comeau in strict and
relaxed mode.


It compiles in MSVC and relaxed comeau, but fails in gcc 4.x and strict
comeau

It seems it is not possible to derive from a template class and pass it
an argument that is a template parameter itself in the derived class.
This would break a lot of my code.
But I could imagine that this is just not the exactly right way to code
it. Does anyone have any hints on this?
Does anyone know, wether it is possible to tell the gcc to compile in
something similar to the comeau relaxed mode?
And finally, can anyone tell, what the standard says? I don't have the
standard unfortunately.

Thanks for your help
Ingo

// main.cpp

template< typename dataT >
struct A
{
typedef dataT DataT;

DataT* _pCol;
int _n;
};

template< typename T >
struct B : A< int >
{
typedef DataT CanISeeIt;

void func( )
{
_pCol;
_n;
}

};

template< typename T >
struct C : A< T >
{
typedef DataT CanISeeIt;

void func( )
{
_pCol;
_n;
}
};

int main( ){}

Just change your C<> class to the correct way:

template< typename T >
struct C : A< T >
{
typedef typename A<T>::DataT CanISeeIt;

void func( )
{
this->_pCol; // or A<T>::_pCol;
this->_n; // or A<T>::_n;
}
};

See this FAQ and the one following:

http://www.parashift.com/c++-faq-lite/templates.html#faq-35.18

Cheers! --M
 
N

nutty



Wow, thank you very much for this quick and excellent reply.

What I learned from this, is to re-read the faq each time before I post
silly questions.

However, I now have a rather academic question.
Why is the standard like this? It seems a little inconsistent to me and
MSVC seems to indicate that it is possible differently. Is there a
catch? Is there a good reason for this?

thank you
Ingo
 
M

mlimber

nutty said:
Wow, thank you very much for this quick and excellent reply.

What I learned from this, is to re-read the faq each time before I post
silly questions.

However, I now have a rather academic question.
Why is the standard like this? It seems a little inconsistent to me and
MSVC seems to indicate that it is possible differently. Is there a
catch? Is there a good reason for this?

See if #2 and #7 - #10 in these template FAQs clear it up:

http://womble.decadentplace.org.uk/c++/template-faq.html

Cheers! --M
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top