gpt4 book ai didi

c++ - 在 std::map 中插入一个 boost::unique_ptr

转载 作者:行者123 更新时间:2023-11-30 03:28:28 25 4
gpt4 key购买 nike

我正在尝试将 boost::unique_ptr 存储在 std::map 中。我使用的编译器是 g++ 3.4 (/usr/bin/g++34 -I./boost_1_62_0/test_boost_unique_ptr.cpp)。

#include <iostream>
#include <map>
#include <boost/move/unique_ptr.hpp>
#include <boost/move/make_unique.hpp>
#include <boost/ptr_container/ptr_map.hpp>

using namespace boost::movelib;
typedef std::map<const int, unique_ptr<int> > MGR_T;
typedef MGR_T::const_iterator MGR_ITER_T;
MGR_T mgrPluginCache;

int main()
{
unique_ptr<int> p (new int(10));
std::cout << "p = " << *p << std::endl;
mgrPluginCache.insert(std::make_pair(1, boost::move(p)));
// mgrPluginCache[0] = (boost::move(p));

return 0;
}

这会导致编译错误:

In function `int main()':
./boost_1_62_0/boost/move/core.hpp:94: error: `boost::rv<T>::rv(const boost::rv<T>&) [with T = boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> >]' is private
test_boost_unique_ptr.cpp:37: error: within this context
test_boost_unique_ptr.cpp:37: error: initializing argument 2 of `std::pair<_T1, _T2> std::make_pair(_T1, _T2) [with _T1 = int, _T2 = boost::rv<boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> > >]'
./boost_1_62_0/boost/move/core.hpp:93: error: `boost::rv<T>::~rv() [with T = boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> >]' is private
test_boost_unique_ptr.cpp:37: error: within this context
test_boost_unique_ptr.cpp: In destructor `std::pair<int, boost::rv<boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> > > >::~pair()':
./boost_1_62_0/boost/move/core.hpp:93: error: `boost::rv<T>::~rv() [with T = boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> >]' is private
test_boost_unique_ptr.cpp:37: error: within this context
test_boost_unique_ptr.cpp: In copy constructor `std::pair<int, boost::rv<boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> > > >::pair(const std::pair<int, boost::rv<boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> > > >&)':
./boost_1_62_0/boost/move/core.hpp:94: error: `boost::rv<T>::rv(const boost::rv<T>&) [with T = boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> >]' is private
test_boost_unique_ptr.cpp:37: error: within this context
./boost_1_62_0/boost/move/core.hpp:93: error: `boost::rv<T>::~rv() [with T = boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> >]' is private
test_boost_unique_ptr.cpp:37: error: within this context
./boost_1_62_0/boost/ptr_container/ptr_map_adapter.hpp: In member function `void boost::ptr_map_adapter<T, VoidPtrMap, CloneAllocator, Ordered>::insert(const Range&) [with Range = std::pair<int, boost::rv<boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> > > >, T = boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> >, VoidPtrMap = std::map<const int, void*, std::less<const int>, std::allocator<std::pair<const int, void*> > >, CloneAllocator = boost::heap_clone_allocator, bool Ordered = true]':
test_boost_unique_ptr.cpp:37: instantiated from here
./boost_1_62_0/boost/ptr_container/ptr_map_adapter.hpp:518: error: no matching function for call to `begin(const std::pair<int, boost::rv<boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> > > >&)'
./boost_1_62_0/boost/ptr_container/ptr_map_adapter.hpp:518: error: no matching function for call to `end(const std::pair<int, boost::rv<boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> > > >&)'
/usr/lib/gcc/x86_64-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/stl_pair.h: In constructor `std::pair<_T1, _T2>::pair(const _T1&, const _T2&) [with _T1 = int, _T2 = boost::rv<boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> > >]':
/usr/lib/gcc/x86_64-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/stl_pair.h:144: instantiated from `std::pair<_T1, _T2> std::make_pair(_T1, _T2) [with _T1 = int, _T2 = boost::rv<boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> > >]'
test_boost_unique_ptr.cpp:37: instantiated from here
./boost_1_62_0/boost/move/core.hpp:94: error: `boost::rv<T>::rv(const boost::rv<T>&) [with T = boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> >]' is private
/usr/lib/gcc/x86_64-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/stl_pair.h:85: error: within this context
/usr/lib/gcc/x86_64-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/stl_pair.h:144: instantiated from `std::pair<_T1, _T2> std::make_pair(_T1, _T2) [with _T1 = int, _T2 = boost::rv<boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> > >]'
test_boost_unique_ptr.cpp:37: instantiated from here
./boost_1_62_0/boost/move/core.hpp:93: error: `boost::rv<T>::~rv() [with T = boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> >]' is private
/usr/lib/gcc/x86_64-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/stl_pair.h:85: error: within this context

有没有办法将 boost::unique_ptr 存储在 std::map 中?早期版本允许吗?我们可以用其他方式做到这一点吗?我不能更改编译器,我正在处理的代码很旧并且很长时间没有修改。

最佳答案

std::map

value_type 需要insert 的复制构造函数。 boost::unique_ptr 有一个私有(private)的复制构造函数(我想它可能已经被删除了)

您可以简单地使用emplace(您还需要std::moveboost::move):

mgrPluginCache.emplace(1, boost::move(foo));

但是...如果你有 std::map::emplace 那么你的编译器支持 c++11,那么你可以考虑使用 std::unique_ptr的 boost 。

附言

g++ 3.4.6 已有 10 年历史,可能会在最近的 boost 版本中破坏某些东西。使用过时的编译器是否有任何特定原因?

关于c++ - 在 std::map 中插入一个 boost::unique_ptr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46562395/

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