Reputation: 3046
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
Upvotes: 0
Views: 180
Reputation: 227370
The problem could be that here:
ThreadLocalPointer<PacketClientHeader*> _incomingBuffer;
the template argument T
is PacketClientHeader*
, and here:
ThreadLocalPointer<T>& operator=(const T* right) { .... }
your operator is taking a const T*
, which would resolve to const PacketClientHeader**
. You then try to assign from a PacketClientHeader*
here:
_incomingBuffer = (PacketClientHeader*)malloc(MAX_DATAGRAM_SIZE);
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
ThreadLocalPointer<PacketClientHeader> _incomingBuffer;
or declaring the assignment operator as
ThreadLocalPointer& operator=(const T& right) { .... }
Upvotes: 3