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.
source
share