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

Discussion in 'C++' started by Frank Bergemann, May 2, 2008.

  1. 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;
    }

    };
     
    Frank Bergemann, May 2, 2008
    #1
    1. Advertising

  2. Frank Bergemann, May 2, 2008
    #2
    1. Advertising

  3. Frank Bergemann

    kwikius Guest

    "Frank Bergemann" <> wrote in message
    news:...
    > 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
     
    kwikius, May 2, 2008
    #3
  4. Frank Bergemann

    peter koch Guest

    On 2 Maj, 11:42, Frank Bergemann <> wrote:
    > 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
     
    peter koch, May 2, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike Smith

    ABC vs. CRTP?

    Mike Smith, Mar 2, 2005, in forum: C++
    Replies:
    7
    Views:
    927
    Dietmar Kuehl
    Mar 3, 2005
  2. Ben
    Replies:
    2
    Views:
    923
  3. oor
    Replies:
    0
    Views:
    1,374
  4. er ci
    Replies:
    1
    Views:
    289
  5. Lawrence D'Oliveiro

    Death To Sub-Sub-Sub-Directories!

    Lawrence D'Oliveiro, May 5, 2011, in forum: Java
    Replies:
    92
    Views:
    2,084
    Lawrence D'Oliveiro
    May 20, 2011
Loading...

Share This Page