在下面的代码示例中,我正在试验 std::unique_ptr。我能够像我期望的那样将 unique_ptr 添加到 map 中。令我惊讶的是我不能让它成为 std::pair 的成员。示例中的注释行应该尝试构建一个与我的 map::value_type 类型相同(我认为...)的对。我不确定为什么这不起作用。
提前致谢。
#include <iostream>
#include <memory>
#include <map>
#include <arpa/inet.h>
typedef std::map<uint32_t, std::unique_ptr<uint32_t> > ntohl_map_type;
typedef std::map<uint32_t, uint32_t> u32_map_type;
void
u32_map()
{
uint32_t key(0);
uint32_t val(0);
u32_map_type u32_map;
u32_map.insert(u32_map_type::value_type(key, val));
u32_map.insert(std::pair<uint32_t, uint32_t>(++key, ++val));
std::cout << "u32_map: " << std::endl;
for (auto &itr : u32_map) {
std::cout << itr.first << " = " << itr.second << "\n";
}
std::cout << std::endl;
}
void
uptr_map()
{
uint32_t key(9);
std::unique_ptr<uint32_t> u32_uptr1(new uint32_t(ntohl(key)));
ntohl_map_type ntohl_map;
ntohl_map.insert(ntohl_map_type::value_type(key, std::move(u32_uptr1)));
++key;
std::unique_ptr<uint32_t> u32_uptr2(new uint32_t(ntohl(key)));
// It seems odd these don't work....
//foo = std::pair<uint32_t, std::unique_ptr<uint32_t>(key, std::move(u32_uptr2));
//ntohl_map.insert(std::pair<uint32_t, std::unique_ptr<uint32_t>(key, std::move(u32_uptr2)));
std::cout << "uptr_map: " << std::endl;
for (auto &itr : ntohl_map) {
std::cout << itr.first << " = " << *itr.second << "\n";
}
}
int
main()
{
u32_map();
uptr_map();
return 0;
}
编辑: 刚刚意识到编译器错误可能会有用:
error: no matching constructor for initialization of 'std::unique_ptr<uint32_t>'
...const, std::unique_ptr<uint32_t>(key, std::move(u32_uptr2)));
^ ~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/c++/v1/memory:2554:31: note: candidate constructor not viable: no known conversionfrom 'uint32_t' (aka 'unsigned int') to 'pointer' (aka 'unsigned int *') for 1st argument; take the address of the argument with &
_LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename conditional<
^
/usr/bin/../lib/c++/v1/memory:2561:31: note: candidate constructor not viable: no known conversion from 'uint32_t' (aka 'unsigned int') to 'pointer' (aka 'unsigned int *') for 1st argument; take the address of the argument with &
_LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename...
最佳答案
您忘记了映射的键是常量(以防止您有意或无意地弄乱关联容器的内部排序):
ntohl_map.insert(
std::pair<uint32_t const, std::unique_ptr<uint32_t>>(
// ^^^^^
key, std::move(u32_uptr2)));
为避免犯错,您可以这样做:
ntohl_map.insert(ntohl_map_type::value_type(key, std::move(u32_uptr2)));
原来调用insert()
的原因从你的问题的文本不编译是因为你提供的对的类型与 insert()
对的类型不同正在接受(因为 const
限定符),必须进行转换,这会导致尝试从您提供的一对中复制构造一个临时对。
复制构造一对意味着复制构造它的元素,并且由于std::unique_ptr
不可复制构造,您的程序无法编译。
函数使用map<uint32_t, uint32_t>
的原因正在编译的是 uint32_t
是(显然)可复制构造的。
另请注意,自 C++11 std::map
起有一个 emplace()
允许就地构造其元素的成员函数(某些实现尚未提供,所以这可能是您的情况):
ntohl_map.emplace(key, std::move(u32_uptr2));
关于c++ - std::pair 和 map::value_type 与 unique_ptr 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17002227/