Arrays with Curiously Recurring Template Pattern?
I have a CRTP-based wrapper for a Windows HANDLE
:
#include <windows.h>
template<class T>
class HandleT
{
HANDLE handle;
operator HANDL开发者_JS百科E() const { return this->handle; }
static ULONG WaitForMultipleObjects(DWORD count, /* ??? */ objects[])
{
return WaitForMultipleObjects(count, ...);
}
};
class EventHandle : Handle<EventHandle>
{
//...
};
class FileHandle : Handle<FileHandle>
{
//...
};
The trouble I'm having is, I have no idea what to substitute for ???
above. I can't say HandleT<T>
, because they can be different kinds of handles, which wouldn't fit into an array. And I don't want to say HANDLE
, because then the function wouldn't be working on HandleT
objects at all -- the user might as well just avoid calling the wrapper entirely. And I can't use variadic templates, because I'm still in the pre-C++0x world.
Is there a known solution to this problem, or do I just have to use a non-ideal solution mentioned above?
You may mix CRTP and polymorphism. See https://stackoverflow.com/a/13868014/1902095
The idea is to have an interface class, BaseHandle
, which would be used as polymorphic pointer within the array in question. HandleT<>
would inherit the above class and implement common methods (and those methods that differ by T).
精彩评论