【问题标题】:Difference between std::pair and map::value_type with unique_ptrstd::pair 和 map::value_type 与 unique_ptr 之间的区别
【发布时间】:2013-06-04 19:56:06
【问题描述】:

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

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

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...

【问题讨论】:

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


    【解决方案1】:

    您忘记了映射的键是常量(以防止您有意或无意地弄乱关联容器的内部顺序):

    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&lt;uint32_t, uint32_t&gt; 的函数正在编译的原因是uint32_t(显然)是可复制构造的。

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

     ntohl_map.emplace(key, std::move(u32_uptr2));
    

    【讨论】:

    • 我认为这不是问题所在。首先,您会注意到我不必在 u32_map 函数中这样做。另外,如果您注意到我已经有了最后一道光(使用 value_type),我只是认为它们是等效的语句,我不明白为什么一个有效而另一个无效。
    • @user2466803:我试图在最后一次编辑答案时解释这一点。请检查更新的答案。另外,试试我的建议 - 你会看到你的代码正在编译。
    • 啊,复制结构确实有意义。尽管您的解决方案仍然无法为我编译。为了避免复制构造,我不得不 std::move temp std::pair,如下所示: ntohl_map.insert(std::move(std::pair>(key, std: :move(u32_uptr2))));
    • @user2466803:但您似乎仍然忘记了const。我在发布之前尝试了我的代码并编译了它(请参阅this live example)。
    • 呃哦....现在我更困惑了 :) 看来 const 甚至都不需要。我终于把你的线放在我的下面,并注意到了不同之处。我的 std::pair 缺少一个“>”。
    猜你喜欢
    • 1970-01-01
    • 2014-10-29
    • 2014-04-29
    • 2013-08-24
    • 2011-03-24
    • 2018-01-05
    • 1970-01-01
    • 2011-10-04
    相关资源
    最近更新 更多