我正在尝试编写一个可移植的线程抽象。现在我有一个编译 dn 的代码可以在 Unix 上工作,但不能在 Windows 上编译(使用 VS2010)。
class Thread
{
public:
Thread();
~Thread();
template<typename Callable, typename Arg>
void startThread(Callable c, Arg a);
void killThread();
private:
template<typename Bind>
struct nested
{
static DWORD WINAPI run(void *obj)
{
Bind * b = reinterpret_cast<Bind *>(obj);
return (b->exec());
}
};
template<typename Callable, typename Arg>
class Binder
{
public:
Binder(Callable c, Arg a): _call(c), _arg(a) {}
~Binder() {}
DWORD operator()() {return (this->_call(this->_arg))}
DWORD exec() {return (this->_call(this->_arg))}
private:
Callable _call;
Arg _arg;
};
HANDLE _handle;
DWORD _id;
bool _isRunning;
DWORD _exitValue;
};
template<typename Callable, typename Arg>
void Thread::startThread(Callable c, Arg a)
{
Thread::Binder<Callable, Arg> *b =
new Thread::Binder<Callable, Arg>(c, a);
CreateThread(0, 0,
Thread::nested< Thread::Binder<Callable, Arg> >::run,
b, 0, &this->_id);
}
当我尝试编译时,VS 给了我一个错误 C2039 :
'nested<Thread::Binder<unsigned long (__cdecl*)(int *),int *> >' : is not a member of 'Thread'
为什么 g++ 能看到,VS 却看不到?大多数情况下,我认为这是因为模板专业化,但怎么会呢?