Initialize std :: map with unique_ptr as value

How to initialize a static map, where is the value std::unique_ptr?

static void f()
{
    static std::map<int, std::unique_ptr<MyClass>> = {
        { 0, std::make_unique<MyClass>() }
    };
}

Of course, this does not work (copy-ctor std::unique_ptris removed).

Is it possible?

+4
source share
3 answers

The problem is that building from std::initializer-listcopies its contents. (objects std::initializer_listinherently const). To solve your problem: you can initialize the card from a separate function ...

std::map<int, std::unique_ptr<MyClass>> init(){
    std::map<int, std::unique_ptr<MyClass>> mp;
    mp[0] = std::make_unique<MyClass>();
    mp[1] = std::make_unique<MyClass>();
    //...etc
    return mp;
}

And then call him

static void f()
{
    static std::map<int, std::unique_ptr<MyClass>> mp = init();
}

Watch Live On Coliru

+6
source

- . , , . auto & decltype, , .

, , , , , - . .

#include <memory>
#include <map>
#include <utility>

struct MyClass {};


static auto& f()
{
  static std::map<int, std::unique_ptr<MyClass>> mp = [](auto& model)
  {
    auto mp = std::decay_t<decltype(model)> {};
    mp.emplace(0, std::make_unique<MyClass>());
    mp.emplace(1, std::make_unique<MyClass>());
    return mp;
  }(mp);
  return mp;
}

int main()
{
  auto& m = f();
}

. elision/RVO.

#include <memory>
#include <map>
#include <utility>

struct MyClass {};

static auto& f()
{
  static auto mp = [](auto mp)
  {
    mp.emplace(0, std::make_unique<MyClass>());
    mp.emplace(1, std::make_unique<MyClass>());
    return mp;
  }(std::map<int, std::unique_ptr<MyClass>>{});
  return mp;
}

int main()
{
  auto& m = f();
}

, .

#include <memory>
#include <map>
#include <utility>

struct MyClass {};

static auto& f()
{
  static auto mp = [mp = std::map<int, std::unique_ptr<MyClass>>{}]() mutable
  {
    mp.emplace(0, std::make_unique<MyClass>());
    mp.emplace(1, std::make_unique<MyClass>());
    return std::move(mp);
  }();
  return mp;
}

int main()
{
  auto& m = f();
}
+1

Writing a custom scratch code seems boring and interferes with clarity.

Here is a fairly efficient generic container initialization code. It stores your data temporarily std::array, as the list of initializers does, but it displays instead const.

make_map takes an even number of elements, the first of which is the second value.

template<class E, std::size_t N>
struct make_container_t{
  std::array<E,N> elements;
  template<class Container>
  operator Container()&&{
    return {
      std::make_move_iterator(begin(elements)),
      std::make_move_iterator(end(elements))
    };
  }
};
template<class E0, class...Es>
make_container_t<E0, 1+sizeof...(Es)>
make_container( E0 e0, Es... es ){
  return {{{std::move(e0), std::move(es)...}}};
}

namespace details{
  template<std::size_t...Is, class K0, class V0, class...Ts>
  make_container_t<std::pair<K0,V0>,sizeof...(Is)>
  make_map( std::index_sequence<Is...>, std::tuple<K0&,V0&,Ts&...> ts ){
    return {{{
      std::make_pair(
        std::move(std::get<Is*2>(ts)),
        std::move(std::get<Is*2+1>(ts) ))
      )...
    }}};
  }
}
template<class...Es>
auto make_map( Es... es ){
  ststic_assert( !(sizeof...(es)&1), "key missing a value?  Try even arguments.");
  return details::make_map(
    std::make_index_sequence<sizeof...(Es)/2>{},
    std::tie( es... )
  );
}

This should reduce it to:

static std::map<int, std::unique_ptr<MyClass>> = 
  make_map(0, std::make_unique<MyClass>());

... typos ban.

+1
source

All Articles