std :: pair和map :: value_type与unique_ptr之间的区别

use*_*803 4 c++ unique-ptr c++11

在下面的代码示例中,我正在尝试使用std :: unique_ptrs.我能够像我期望的那样将unique_ptr添加到地图中.令我惊讶的是,我无法使它成为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;
}
Run Code Online (Sandbox Code Playgroud)

编辑:刚刚意识到编译器错误可能会有用:

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...
Run Code Online (Sandbox Code Playgroud)

And*_*owl 6

您忘记了地图的关键点是不变的(以防止您有意或无意地弄乱关联容器的内部顺序):

ntohl_map.insert(
    std::pair<uint32_t const, std::unique_ptr<uint32_t>>(
//                     ^^^^^
        key, std::move(u32_uptr2)));
Run Code Online (Sandbox Code Playgroud)

为了避免犯这个错误,你可以做到:

ntohl_map.insert(ntohl_map_type::value_type(key, std::move(u32_uptr2)));
Run Code Online (Sandbox Code Playgroud)

insert()从您的问题文本中原始调用不能编译的原因是,由于您提供的对的类型与正在接受的对的类型不同insert()(因为该const限定符),因此必须进行转换,这会导致尝试从您提供的临时对中复制构造一个临时对.

复制构造一对意味着复制构造其元素,并且由于std::unique_ptr不是可复制构造的,因此您的程序无法编译.

使用a的函数map<uint32_t, uint32_t>编译的原因uint32_t是(显然)可复制构造.

另请注意,由于C++ 11 std::map具有emplace()成员函数(某些实现尚未提供,因此可能是您的情况),它允许就地构造其元素:

 ntohl_map.emplace(key, std::move(u32_uptr2));
Run Code Online (Sandbox Code Playgroud)