CRTP how to pass type

This is a discussion on CRTP how to pass type within the C++ Programming forums, part of the General Programming Boards category; I'm trying to use CRTP . But I'm failing at most basic stuff: Code: template<class Derived> class Base { public: ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    6

    CRTP how to pass type

    I'm trying to use CRTP.

    But I'm failing at most basic stuff:
    Code:
    template<class Derived>
    class Base
    {
    public:
        typename Derived::TYPE i;
    };
    
    template<typename T>
    class Derived : public Base<Derived<T>>
    {
    public:
        typedef T TYPE;
    };

    I'm getting the compiler error:
    Code:
    'TYPE' is not a member of Derived<T>

    But I think it is. What's the problem?
    The issue is that the compiler does'nt know the definition of class Derived yet (at compile time). But I'm searching for a solution. Any ideas?
    Last edited by FrEEzE2046; 12-08-2009 at 07:30 AM.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I think it is not going to be able to use that typedef, because at that point where you need it (in Base), Derived is still incomplete.

    For one thing you could just use two template arguments (of course if you need the Derived argument in the first place):

    Code:
    template<class Derived, class T>
    class Base
    {
    public:
        T i;
    };
    
    template<typename T>
    class Derived : public Base<Derived<T>, T>
    {
    public:
        //typedef T TYPE;
    };
    Another option might be to use a typetraits class that is able to extract the type from the template:

    Code:
    template <class T>
    struct extract_type
    {
    };
    
    template <template <class> class A, class T>
    struct extract_type<A<T>>
    {
        typedef T type;
    };
    
    template< class Derived>
    class Base
    {
    public:
        typename extract_type<Derived>::type i;
    };
    
    template<typename T>
    class Derived : public Base<Derived<T>>
    {
    public:
        //typedef T TYPE;
    };
    
    int main()
    {
        Derived<int> d;
        d.i = 10;
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf oddities
    By robwhit in forum C Programming
    Replies: 5
    Last Post: 09-22-2007, 01:03 AM
  2. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 03:44 AM
  3. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  4. Changing an objects Type
    By ventolin in forum C++ Programming
    Replies: 2
    Last Post: 06-29-2004, 07:18 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21