CRTP question

Discussion in 'C++' started by michael.alexeev@qwest.com, Apr 17, 2006.

  1. Guest

    Hi all,

    Consider the following program:

    template <class T>
    struct Traits{
    typedef typename T::type_t type_t; //line 3
    };

    template <class Derived>
    struct Base{
    typedef typename Traits<Derived>::type_t type_t; // line 8
    type_t foo() {
    Derived& self = static_cast<Derived&>(*this);
    return self.bar();
    }

    };

    template<typename T>
    struct Derived : public Base<Derived<T> >{
    typedef T type_t;
    type_t bar() {
    return type_t();
    }
    };

    int main(){
    Derived<int> d;
    return 0;
    }

    Compiler (gcc 4.0.2) complains with the following message:

    t.cxx: In instantiation of Traits<Derived<int> >:
    t.cxx:8: instantiated from Base<Derived<int> >
    t.cxx:17: instantiated from Derived<int>
    t.cxx:25: instantiated from here
    t.cxx:3: error: no type named type_t in struct Derived<int>

    I was under the impresion that declarations in the Base class template
    are instantiated when the Derived class is declared which means that
    Derived::type_t is accessable. Looks like it's not a case.

    Any help/walkaround is greatly appreciated.

    Mike
     
    , Apr 17, 2006
    #1
    1. Advertising

  2. wrote:
    > Consider the following program:
    >
    > template <class T>
    > struct Traits{
    > typedef typename T::type_t type_t; //line 3
    > };
    >
    > template <class Derived>
    > struct Base{
    > typedef typename Traits<Derived>::type_t type_t; // line 8
    > type_t foo() {
    > Derived& self = static_cast<Derived&>(*this);
    > return self.bar();
    > }
    >
    > };
    >
    > template<typename T>
    > struct Derived : public Base<Derived<T> >{
    > typedef T type_t;
    > type_t bar() {
    > return type_t();
    > }
    > };
    >
    > int main(){
    > Derived<int> d;
    > return 0;
    > }
    >
    > Compiler (gcc 4.0.2) complains with the following message:
    >
    > t.cxx: In instantiation of Traits<Derived<int> >:
    > t.cxx:8: instantiated from Base<Derived<int> >
    > t.cxx:17: instantiated from Derived<int>
    > t.cxx:25: instantiated from here
    > t.cxx:3: error: no type named type_t in struct Derived<int>
    >
    > I was under the impresion that declarations in the Base class template
    > are instantiated when the Derived class is declared which means that
    > Derived::type_t is accessable. Looks like it's not a case.


    The problem is that by the time it gets to use 'Derived<int>::type_t',
    during instantiating of Derived<int>::Base<Derived<int> >, the inner
    contents of 'Derived<int>' haven't been reached yet, so none of its
    members are known.

    > Any help/walkaround is greatly appreciated.


    You could try specialising 'Traits' this way:

    template<class T> class Derived; // forward-delcaration
    template<class T> class Traits<Derived<T> > { typedef T type_t; };

    ....since you know that 'type_t' is going to be the same as T. And then
    declare your Base, and so forth.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Apr 17, 2006
    #2
    1. Advertising

  3. Guest

    Thanks Victor,

    That's did the trick.

    Mike
     
    , Apr 17, 2006
    #3
    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:
    935
    Dietmar Kuehl
    Mar 3, 2005
  2. fabioppp

    CRTP question

    fabioppp, Apr 15, 2005, in forum: C++
    Replies:
    2
    Views:
    395
    James Aguilar
    Apr 15, 2005
  3. CRTP and Factories

    , Jul 25, 2006, in forum: C++
    Replies:
    2
    Views:
    344
  4. Noah Roberts

    CRTP

    Noah Roberts, Jan 12, 2007, in forum: C++
    Replies:
    6
    Views:
    915
    kwikius
    Jan 15, 2007
  5. Arash Partow

    Heterogeneous containers with CRTP

    Arash Partow, May 15, 2007, in forum: C++
    Replies:
    2
    Views:
    773
    Axter
    May 15, 2007
Loading...

Share This Page