0
#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のコピー要素。しかし、push_backフォームが動作します。差の演算子[](KのCONST&)<Kは、ptr_deque < T > ::ブースト>上記の例では

私はoperator [] (K const &)は、どうやらそれはそうではありません基本的にコメントアウト文

である、単にreturn emplace(k, mapped_type()).first->secondまたはreturn insert(value_type(k, mapped_type())).first->secondであることを考えていました。 operator []は内部でplacement newの魔法を実行しますか?

また、ptr_dequeには特別なものがありますか?私はhttp://en.cppreference.com/w/cpp/container/unordered_map/operator_atによると、GCC-6.1 &ブースト1.59

+0

とほぼ同等である;' – aschepler

+0

ありがとう。これが返信であった場合は、私は投票して回答とします – zrb

答えて

2

を使用しています

2)は、キーが存在しない場合 std::piecewise_construct, std::forward_as_tuple(std::move(key)), std::tuple<>() からその場で構築さvalue_typeオブジェクトを挿入します。

(私はあなたがoperator[]の引数として右辺値を使用している、コメントアウト行であるためKey&&過負荷を参照しています。けれどもKey=intの場合に違いはかなり簡単です。)

あなたの質問を参考にそう

operator[](Key&&)は `testum.emplace(STD :: piecewise_construct、STD :: make_tuple(1U)、STD :: make_tuple())も試してみてください

return emplace(std::piecewise_construct, 
       std::forward_as_tuple(std::move(k)), 
       std::tuple<>()).first->second; 
関連する問題