我构建了一个模板类,它应该代表一个线程本地指针。
template <class T>
class ThreadLocalPointer {
public:
T& operator*() {
return *_map[std::this_thread::get_id()];
}
T* operator->() {
return _map[std::this_thread::get_id()];
}
ThreadLocalPointer<T>& operator=(const T* right) {
_map[std::this_thread::get_id()] = right;
return *this;
}
T* get() {
return _map[std::this_thread::get_id()];
}
private:
std::unordered_map<std::thread::id, T*> _map;
};
类的实例:
ThreadLocalPointer<PacketClientHeader*> _incomingBuffer;
最后,我想在哪里使用我的赋值运算符
_incomingBuffer = (PacketClientHeader*)malloc(MAX_DATAGRAM_SIZE);
代码不会编译:
no operator found which takes a right-hand operand of type 'PacketClientHeader *' (or there is no acceptable conversion)
我不确定我做错了什么!
编辑:包括完整的模板