CRTP base class using typedef of sub-class doesn't work

  • Thread starter Frank Bergemann
  • Start date
F

Frank Bergemann

Hi,

the (gcc-3.4.4) compiler complains, if i try to use a typedef of
subclass in superclass:

|padsol15 141> make Helper_test
[CXXD] Helper_test.cc
In file included from Helper_test.cc:17:
Helper.h:258: error: ISO C++ forbids declaration of `BaseType' with no
type
Helper.h:258: error: `::BaseType' is not a valid declarator
Helper.h:258: error: expected `;' before "BaseType"
make: *** [Helper_test.o] Error 1

???


*
* internal base type for shared basics
*/
template<typename X>
class _common
{
public:
typedef X ForType;
typedef X::BaseType BaseType;
typedef _common Super;

BaseType m_data;
/*
operators based on STL predicator specializations of BaseType,
etc, ...
*/
} ;

class wrapper : public _common<wrapper>
{
public:
typedef wrapped_t BaseType; // _common<> can't use that?!

wrapper (wrapped_t data) : Super(data) { /* void */ }
wrapper (wrapper const& rhs) : Super(rhs.m_data) { /* void */ }
~wrapper() { /* void */ }

wrapper& operator=(wrapper const& rhs)
{
if (&rhs != this) {
wrapper x(rhs);
Super::swap(x);
}
return *this;
}

};
 
K

kwikius

Frank Bergemann said:
Hi,

the (gcc-3.4.4) compiler complains, if i try to use a typedef of
subclass in superclass:

|padsol15 141> make Helper_test
[CXXD] Helper_test.cc
In file included from Helper_test.cc:17:
Helper.h:258: error: ISO C++ forbids declaration of `BaseType' with no
type
Helper.h:258: error: `::BaseType' is not a valid declarator
Helper.h:258: error: expected `;' before "BaseType"
make: *** [Helper_test.o] Error 1

* internal base type for shared basics
*/
template<typename X>
class _common
{
public:
typedef X ForType;
typedef X::BaseType BaseType;

^^^
<...>
try:

typedef typename X::BaseType BaseType;

regards
Andy Little
 
P

peter koch

Hi,

the (gcc-3.4.4) compiler complains, if i try to use a typedef of
subclass in superclass:
[SNIP]
template<typename X>
class _common
{
public:
        typedef X ForType;
        typedef X::BaseType BaseType;

[snip]

As others already pointed out, this is because the compiler can't
detect that X::BaseType is a type: remember the typename keyword.
Perhaps your question should be dubbed Curiously Recurring Template
Question?

/Peter
 

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,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top