Trouble constructing a map with immovable value types from initializer list

37 Views Asked by At

Consider the following code:

#include <map>
#include <type_traits>
#include <memory>

void foo() {
    using map_type = std::map<int, std::unique_ptr<float[]>>;
    auto my_map = map_type {
        map_type::value_type {
            123, std::make_unique<float[]>(123)
        }
    };
}

This fails to compile (GodBolt), with:

error: no matching function for call to 'construct_at(std::pair<const int, std::unique_ptr<float []> >*&, const std::pair<const int, std::unique_ptr<float []> >&)'

This would "make sense" if I passed an lvalue reference to a unique pointer - but as far as I can tell, I'm passing a perfectly legit prvalue.

Can I massage this code, and particularly the initializer list, so as to be acceptable to the map constructor?

0

There are 0 best solutions below