transferring object ownership on std::allocator rebind
-
30-10-2019 - |
Question
I have a Visual Studio 2008 C++ application where I am implementing a replacement for the standard allocator used in containers like std::vector
. But, I've run in to an issue. My implementation relies on the allocator owning a handle to a resource. In the case where the rebind
feature is used, I would need to transfer ownership of the handle to the new allocator. Something like this:
template< class T >
class MyAllocator
{
public:
template< class U >
explicit MyAllocator( const MyAllocator< U >& other ) throw()
: h_( other.Detach() ) // can't do this to a `const`
{
};
// ...
private:
HANDLE Detach()
{
HANDLE h = h_;
h_ = NULL;
return h;
};
HANDLE h_;
}; // class MyAllocator
Unfortunately, I can't relieve the old allocator of the handle ownership because it is const
. If I remove const
from the rebind constructor, then the containers won't accept it.
error C2558: class 'MyAllocator<T>' : no copy constructor available or copy constructor is declared 'explicit'
Is there a good way around this issue?
No correct solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow