我有一个 Visual Studio 2008 C++ 应用程序,我正在实现一个替换容器中使用的标准分配器,如std::vector
. 但是,我遇到了一个问题。我的实现依赖于拥有资源句柄的分配器。在使用该功能的情况下rebind
,我需要将句柄的所有权转移给新的分配器。像这样的东西:
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
不幸的是,我无法解除句柄所有权的旧分配器,因为它是const
. 如果我const
从重新绑定构造函数中删除,那么容器将不会接受它。
error C2558: class 'MyAllocator<T>' : no copy constructor available or copy constructor is declared 'explicit'
有没有解决这个问题的好方法?