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
问题描述
#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<>()
如果键不存在。
(我指的是 因此,对于你的问题, In the above example, the commented out lines don't compile as they try to copy elements of the I was thinking that Apparently that is not the case. Does Or is there something special about 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 (I'm referring to the So in reference to your question,
这篇关于std :: unordered_map& K,boost :: ptr_deque< T> >的运算符[](K const&)和emplace的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!键&&
重载,因为在注释行中,您使用右值作为参数
。 >
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());
}
ptr_deque
which are non-copyable. However, the push_back
form works.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 statementsoperator []
perform some placement new
magic internally?ptr_deque
?
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.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.)operator[](Key&&)
is roughly equivalent toreturn emplace(std::piecewise_construct,
std::forward_as_tuple(std::move(k)),
std::tuple<>()).first->second;