mercredi 24 août 2016

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;
};

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

Aucun commentaire:

Enregistrer un commentaire