CRTP question

F

fabioppp

Is this piece of code correct?

template<typename Child>
struct Base
{
enum { derived_id = Child::id }; // <= wont compile!

void invokeDispatch()
{
((Child*) this)->invoke(); // <= this compile fine!
}
};

struct Derived1 : public Base<Type1>
{
enum { id = 1 };

void invoke()
{

}
};

Why the enum doesn't compile, while invokeDispatch seems to be fine?

Thanks
 
F

fabioppp

fabioppp said:
struct Derived1 : public Base<Type1>
{
enum { id = 1 };

void invoke()
{

}
};


Sorry... it's:

struct Derived1 : public Base<Derived1>
{
enum { id = 1 };

void invoke()
{

}
};
 
J

James Aguilar

fabioppp said:
struct Derived1 : public Base<Derived1>
{
enum { id = 1 };
void invoke()
{}
};

I'm a relative newbie compared to some of these guys, but it seems to me
like you can't use a class in its own definition. How can the compiler know
what Derived1 does until you finish defining it?

- JFA1
 

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,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top