gpt4 book ai didi

c++ - 具有自定义删除器类的 STL vector 和 unique_ptr

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:54:36 25 4
gpt4 key购买 nike

我有一些 c++ 代码可以用 clang 3.2-7 和 gcc 4.8.1 编译,但不能用 gcc 4.6.3 编译。我正在使用 c++0x 进行编译。

我正在实现一个管理一组固定资源的“ResourcePool”。它通过提供带有自定义删除器类的 unique_ptr 来分配资源;当请求资源的对象超出范围时,删除器将资源返回到池中。

删除器类如下所示:

template <typename T>
class ResourcePoolManager {
public:
ResourcePoolManager(ResourcePool<T> & pool)
: pool(pool)
{
}

~ResourcePoolManager() {};

void operator()(T* releasedResource) const {
pool.releaseResource(releasedResource);
}
private:
ResourcePool<T> & pool;
};

池本身看起来像(编辑掉不相关的方法):

template <typename T>
class ResourcePool {
public:
ResourcePool()
: manager(*this)
{
}

std::unique_ptr<T, ResourcePoolManager<T>> requestResource() {
if(availableResources.size() == 0) {
return std::unique_ptr<T, ResourcePoolManager<T>>(NULL, manager);
} else {
T * resource = availableResources.front();
availableResources.pop_front();
return std::unique_ptr<T, ResourcePoolManager<T>>(resource, manager);
}
}
private:
friend class ResourcePoolManager<T>;

void releaseResource(T * releasedResource) {
availableResources.push_back(releasedResource);
}

ResourcePoolManager<T> manager;
std::deque<T *> availableResources;
std::deque<T *> allResources;
};

当我尝试从池中提取资源并将它们存储在 std::vector; 中时,问题就出现了,其中 OpenCLDevice 是有问题的资源:

ResourcePool<OpenCLDevice> computeDevicePool;
std::unique_ptr<OpenCLDevice, ResourcePoolManager<OpenCLDevice>> devicePtr = computeDevicePool.requestResource();
std::vector<std::unique_ptr<OpenCLDevice, ResourcePoolManager<OpenCLDevice>>> gpus;
gpus.push_back(std::move(devicePtr));

如上文所述,clang 和较新版本的 gcc 都适用于此。然而 gcc 4.6.3 即将推出:

/usr/include/c++/4.6/bits/unique_ptr.h: In member function 'std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>::operator=(std::unique_ptr<_Tp, _Dp>&&) [with _Tp = OpenCLDevice, _Dp = ResourcePoolManager<OpenCLDevice>, std::unique_ptr<_Tp, _Dp> = std::unique_ptr<OpenCLDevice, ResourcePoolManager<OpenCLDevice> >]':
/usr/include/c++/4.6/bits/vector.tcc:319:4: instantiated from 'void std::vector<_Tp, _Alloc>::_M_insert_aux(std::vector<_Tp, _Alloc>::iterator, _Args&& ...) [with _Args = {std::unique_ptr<OpenCLDevice, ResourcePoolManager<OpenCLDevice> >}, _Tp = std::unique_ptr<OpenCLDevice, ResourcePoolManager<OpenCLDevice> >, _Alloc = std::allocator<std::unique_ptr<OpenCLDevice, ResourcePoolManager<OpenCLDevice> > >, std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<std::unique_ptr<OpenCLDevice, ResourcePoolManager<OpenCLDevice> >*, std::vector<std::unique_ptr<OpenCLDevice, ResourcePoolManager<OpenCLDevice> > > >, typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer = std::unique_ptr<OpenCLDevice, ResourcePoolManager<OpenCLDevice> >*]'
/usr/include/c++/4.6/bits/vector.tcc:102:4: instantiated from 'void std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {std::unique_ptr<OpenCLDevice, ResourcePoolManager<OpenCLDevice> >}, _Tp = std::unique_ptr<OpenCLDevice, ResourcePoolManager<OpenCLDevice> >, _Alloc = std::allocator<std::unique_ptr<OpenCLDevice, ResourcePoolManager<OpenCLDevice> > >]'
/usr/include/c++/4.6/bits/stl_vector.h:840:9: instantiated from 'void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = std::unique_ptr<OpenCLDevice, ResourcePoolManager<OpenCLDevice> >, _Alloc = std::allocator<std::unique_ptr<OpenCLDevice, ResourcePoolManager<OpenCLDevice> > >, std::vector<_Tp, _Alloc>::value_type = std::unique_ptr<OpenCLDevice, ResourcePoolManager<OpenCLDevice> >]'
trunk/src/dpa/distinguishers/GenericOpenCLDPA.cpp:109:39: instantiated from here
/usr/include/c++/4.6/bits/unique_ptr.h:176:2: error: use of deleted function 'ResourcePoolManager<OpenCLDevice>& ResourcePoolManager<OpenCLDevice>::operator=(const ResourcePoolManager<OpenCLDevice>&)'

ResourcePool.hpp:30:7 error: 'ResourcePoolManager<OpenCLDevice> & ResourcePoolManager<OpenCLDevice>::operator=(const ResourcePoolManager<OpenCLDevice>&)'
is implicitly deleted because the default definition would be ill-formed:
ResourcePool.hpp:30:7: error: non-static reference member 'ResourcePool<OpenCLDevice>& ResourcePoolManager<OpenCLDevice>::pool', can't use default assignment operator

编译器在指针移入 gpus vector 的最后一行停止,向我表明正在复制不应该复制的内容。第 30 行是 ResourcePoolManager 类 class ResourcePoolManager { 的定义。

我是否在自定义删除器的管理上做错了什么,而新的编译器却让我逍遥法外?

最佳答案

看起来像 libstdc++ 4.6 unique_ptr 中的错误,它要求删除器类型是可复制的。 Changing ResourcePoolManager::pool to a pointer instead of a reference is an effective workaround .

更具体地说,这个程序重现了这个错误:

#include <memory>
#include <vector>

struct deleter {
deleter() : c(*"") {}
void operator () (int *) const {}
deleter(deleter&&) = default;
deleter& operator = (deleter&&) = default;
const char& c;
};

int main() {
std::vector<std::unique_ptr<int, deleter>> vec;
vec.push_back(std::unique_ptr<int, deleter>{});
}

简单地声明 vector 就可以了,它是 push_back 的实例化失败了。

澄清:问题似乎是 vector 的 4.6 实现在调整大小时使用 = 移动分配元素,而 4.8 实现仅使用移动构造.具有引用成员的类 - 例如您的删除器 - 是可移动构造的,但不可移动(或复制,就此而言)可分配。 You can see the same error message in 4.8 if you assign to the vector :

int main() {
std::vector<std::unique_ptr<int, deleter>> vec;
vec.push_back(std::unique_ptr<int, deleter>{new int(42)});
vec[0] = std::unique_ptr<int, deleter>{new int(42)};
}

std::vector::push_back被指定为仅要求 TMoveInsertable,因此我相信这肯定是 libstdc++ 4.6 中的错误。

关于c++ - 具有自定义删除器类的 STL vector 和 unique_ptr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21859798/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com