I have a key value map constexprthat roughly has this definition:
template<typename K, typename V, size_t pos>
class Map {
public:
template<class Head, class... Tail>
constexpr Map(Head head, Tail... tail)
:
value{head},
tail{tail...} {}
Element<K, V> value;
const Map<K, V, pos - 1> tail;
};
template<typename K, typename V>
class Map<K, V, 0> {
public:
constexpr Map() {}
};
To initialize the key value map at compile time, I have a utility function that forwards elements:
template<typename K, typename V, typename... Entries>
constexpr Map<K, V, sizeof...(Entries)> create_const_map(Entries&&... entry) {
return Map<K, V, sizeof...(entry)>(Entry<K, V>{std::forward<Entries>(entry)}...);
}
Element defined as:
template<class K, class V>
class Entry {
public:
constexpr Entry(const K &key, const V &value)
:
key{key},
value{value} {}
constexpr Entry(std::pair<K, V> pair)
:
key{pair.first},
value{pair.second} {}
const K key;
const V value;
};
To create a map, I can successfully use std::make_pair:
constexpr auto cmap = create_const_map<int, int>(
std::make_pair(0, 0),
std::make_pair(13, 37),
std::make_pair(42, 9001)
);
Now I want to eliminate the calls std::make_pairand use curly braces instead:
constexpr auto cmap = create_const_map<int, int>(
{0, 0},
{13, 37},
{42, 9001}
);
But this leads to compilation failure, since {}it is not output as a construction for pairs:
/home/jj/devel/openage/libopenage/datastructure/tests.cpp:191:24: error: no matching function for call to 'create_const_map'
constexpr auto cmap = create_const_map<int, int>(
^~~~~~~~~~~~~~~~~~~~~~~~~~
/home/jj/devel/openage/libopenage/datastructure/constexpr_map.h:286:46: note: candidate function not viable: requires 0 arguments, but 3 were provided
constexpr Map<K, V, sizeof...(Entries)> create_const_map(Entries&&... entry) {
^
What can I do to {a, b}be able to use instead std::make_pair(a, b)?