The CRTP pattern and member function pointer template parameter
Hi,
I wanted to achieve static polymorphism with the CRTP pattern to
create a base class for encapsulating the thread creation with the
pthread API. What I was trying was:
template<class D, void *(D::*F)()>
class ThreadBase
{
static void *start_routine(void *arg);
};
template<class D, void *(D::*F)()>
void *ThreadBase<D,F>::start_routine(void *arg)
{
D *pThis = static_cast<D *>(arg);
return (pThis->*F)();
}
where I could have defined a derived class:
class Derived : public ThreadBase<Derived,&Derived::Run>
{
void *Run();
};
but it does not work. GCC complains that:
incomplete type `Derived' used in nested name specifier.
Right now, I am blinded with my initial design that does not work so I
would like to ask this newsgroup readers for suggestions about the
best way solve my problem.
As a temporary fix, I am going to remove the second parameter F from
the template ThreadBase and hardcode which member function gets called
like this:
template<class D>
void *ThreadBase<D>::start_routine(void *arg)
{
D *pThis = static_cast<D *>(arg);
return pThis->Run();
}
but I find it sad to lose the flexibility to choose at compile time
which member function will be called.
Thank you,
Olivier Langlois
http://www.olivierlanglois.net
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]