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

시도'testum.emplace 거의 비슷하다 (표준 : piecewise_construct, 표준 : : make_tuple과 (1U), 표준 : : make_tuple과());' – aschepler

+0

감사. 이것이 회신 이었다면, 나는 투표를하고 답변으로 표시했다. – zrb

답변

2

을 사용하고

:

2) 키가 존재하지 않는 경우 std::piecewise_construct, std::forward_as_tuple(std::move(key)), std::tuple<>() 에서 장소 구성된 value_type 객체를 삽입합니다.

은 (난 당신이 operator[]에 인수로를 rvalue를 사용하고, 주석 처리 된 라인에서부터 Key&& 과부하 말하는 겁니다. 비록 Key=int의 경우 차이가 거의 사소한입니다.)

그래서 귀하의 질문에 대한 참조에

operator[](Key&&)

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