I build a template class that is supposed to represent a thread-local pointer.
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;
};
Instance of the class:
ThreadLocalPointer<PacketClientHeader*> _incomingBuffer;
And finally, where I want to use my assignment operator
_incomingBuffer = (PacketClientHeader*)malloc(MAX_DATAGRAM_SIZE);
The code won’t compile:
no operator found which takes a right-hand operand of type 'PacketClientHeader *' (or there is no acceptable conversion)
I’m not sure what I’m doing wrong!
Edit: Included the full template
The problem could be that here:
the template argument
TisPacketClientHeader*, and here:your operator is taking a
const T*, which would resolve toconst PacketClientHeader**. You then try to assign from aPacketClientHeader*here:and no suitable operator is found. Without knowing more details of the class it is difficult to suggest a solution, but it could be as simple as using
or declaring the assignment operator as