std :: unordered_map& K,boost :: ptr_deque< T> >的运算符[](K const&)和emplace [英] Difference between std::unordered_map < K, boost::ptr_deque < T > >'s operator[] (K const &) and emplace

查看:401
本文介绍了std :: unordered_map& K,boost :: ptr_deque< T> >的运算符[](K const&)和emplace的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  #include< memory> 
#include< unordered_map>
#include< vector>
#include< utility>
#include< boost / ptr_container / ptr_deque.hpp>

struct T
{
T()= default;
T(T const&)= delete;
T& operator =(T const&)= delete;
T(T&&)= default;
T& operator =(T&&)= default;
};

使用S = boost :: ptr_deque< T。

int main()
{
std :: unordered_map< uint32_t,S>试验
// testum.emplace(1u,S());
// testum.insert(std :: make_pair(1u,S()));
testum [1] .push_back(new T());
}

在上面的示例中,注释掉的行不会以复制不可复制的 ptr_deque 的元素。但是, push_back 表单工作。



我认为 operator [] const&)只是 return emplace(k,mapped_type())。first-> second (value_type(k,mapped_type()))。first-> second ,这本质上是注释的语句



显然不是案子。 在内部执行一些放置新魔术?



或者是有什么特别的 ptr_deque



我使用gcc-6.1& boost 1.59

解决方案

根据 http://en.cppreference.com/w/cpp/container/unordered_map/operator_at


2)从
std :: piecewise_construct,std :: forward_as_tuple()插入一个 value_type std :: move(key)),std :: tuple<>()
如果键不存在。


(我指的是键&& 重载,因为在注释行中,您使用右值作为参数 。 >

因此,对于你的问题, operator [](Key&& amp;)大致相当于

  return emplace(std :: piecewise_construct,
std :: forward_as_tuple(std :: move(k)),
std :: tuple())。first-> second;


#include <memory>
#include <unordered_map>
#include <vector>
#include <utility>
#include <boost/ptr_container/ptr_deque.hpp>

struct T
{
    T() = default;
    T(T const &) = delete;
    T & operator = (T const &) = delete;
    T(T &&) = default;
    T & operator = (T &&) = default;
};

using S = boost::ptr_deque < T >;

int main()
{
    std::unordered_map < uint32_t, S > testum;
    //  testum.emplace(1u, S());
    //  testum.insert(std::make_pair(1u, S()));
    testum[1].push_back(new T());
}

In the above example, the commented out lines don't compile as they try to copy elements of the ptr_deque which are non-copyable. However, the push_back form works.

I was thinking that operator [] (K const &) is simply return emplace(k, mapped_type()).first->second or return insert(value_type(k, mapped_type())).first->second, which is essentially the commented out statements

Apparently that is not the case. Does operator [] perform some placement new magic internally?

Or is there something special about ptr_deque?

I am using gcc-6.1 & boost 1.59

解决方案

According to http://en.cppreference.com/w/cpp/container/unordered_map/operator_at :

2) Inserts a value_type object constructed in-place from std::piecewise_construct, std::forward_as_tuple(std::move(key)), std::tuple<>() if the key does not exist.

(I'm referring to the Key&& overload since in the commented out line, you're using an rvalue as an argument to operator[]. Though in the case of Key=int the difference is pretty much trivial.)

So in reference to your question, operator[](Key&&) is roughly equivalent to

return emplace(std::piecewise_construct,
               std::forward_as_tuple(std::move(k)),
               std::tuple<>()).first->second;

这篇关于std :: unordered_map& K,boost :: ptr_deque&lt; T> &gt;的运算符[](K const&amp;)和emplace的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆