c++ - std::pair 和 map::value_type 与 unique_ptr 的区别

标签 c++ c++11 unique-ptr

在下面的代码示例中,我正在试验 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/

相关文章:

c++ - 如何将二维矩阵传递给函数

c++ - C++ new operator 在下面使用 malloc() 吗?

c++ - glm 和 cxx-prettyprint,clang : call to function 'operator <<' that is neither visible in the template definition

c++ - 在不同浮点精度之间切换时使用我自己的 std::mersenne_twister_engine 模板参数

C++:带有删除器的类的前向声明,用于可重复的唯一指针

c++ - 如何将 lambda 与模板化 std::unique_ptr 一起使用?

c++ - 将 unique_ptr 转移到另一个的有效和/或正确的方法是什么?

c++ - 我们可以说这是一个简单的 DDOS 僵尸网络吗?

c++ - 为什么 unique_ptr 析构函数的异常会终止程序?

c++ - 引用派生类时基类的对象大小