simple question, how initialize map of arrays (or other container type) of different sizes? example:
enum class code {a,b,c}; enum class res {x1,x2,x3,x4,x5}; std::map<code, ?> name { {code:a, {res::x1,res::x2}}, {code:b, {res::x2,res::x3, res::x4}}, {code:c, {res::x5}} };
i need find @ compile time if res::x2
in map name
@ code::b
this expression should, think, check using static_assert
:
constexpr bool validate(code t, res p, int = 0) { return (name[t][i] == p ? true : ((sizeof(name[t]) == (i+1)) ? false :validate(t, p, ++i))); }
because validate
constexpr
basic array work how define in map argument array of type res
? , each array different in size?
so, i've made mistake. under impression map can accessed in constexpr function. container type can suggest me allow me achieve have written above?
if ok array const, works:
std::map<code, std::initializer_list<res>> name { {code::a, {res::x1,res::x2}}, {code::b, {res::x2,res::x3, res::x4}}, {code::c, {res::x5}} };
if need able write array, you'll need this:
std::map<code, std::vector<res>> name { {code::a, {res::x1,res::x2}}, {code::b, {res::x2,res::x3, res::x4}}, {code::c, {res::x5}} };
this come @ cost of additional memory allocations, since vector allocated on heap.
if want writable , ok fixed size, works , avoids additional allocations:
std::map<code, std::array<res, 3>> name { {code::a, {res::x1,res::x2}}, {code::b, {res::x2,res::x3, res::x4}}, {code::c, {res::x5}} };
as second part of question, i'm not quite sure if any solution work, given cannot access map in constexpr function.
Comments
Post a Comment