c++ - (VS2015) Attempting to fill a static map with data from initializer list -
i have class sorta like:
class object { public: struct flag { const uint32_t bit = 0; const wchar_t* name = l""; const wchar_t sign = l""; } static std::map<const char*, flag> flags; }
i'm on vs2015, want support clang , gcc (the latest). problem is, can't figure out how initialize map data.
i tried putting inline, like:
static std::map<const char*, flag> flags = { { "foo1", { 0, l"foo1", l'a' } }, { "foo2", { 1, l"foo3", l'b' } }, { "foo3", { 2, l"foo3", l'c' } } }
but complained const integral types can in-class. okay! left declaration in class definition (as shown in first code snippet), , put in associated cpp:
static std::map<const char*, object::flag> object::flags = { { "foo1", { 0, l"foo1", l'a' } }, { "foo2", { 1, l"foo3", l'b' } }, { "foo3", { 2, l"foo3", l'c' } } }
now complains that:
error c2440: 'initializing': cannot convert 'initializer list' 'std::map,std::allocator>>'
the thing is, could've sworn i've had working, i'm thinking must have syntax wrong. if not, i'm missing how load static map classes namespace.
according c++11 standard, flag
not aggregate due presence of brace-or-equal-initializers (aka default member initializers), attempting use aggregate initialization initialize fails. c++14 removed restriction, flag
considered aggregate according version of standard, , your code valid.
here's simpler version of example fails compile -std=c++11
, compiles -std=c++14
.
#include <stdint.h> struct flag { const uint32_t bit = 0; const wchar_t* name = l""; const wchar_t sign = l' '; }; int main() { flag f{ 0u, l"foo1", l'a' }; }
vs2015 still has c++11 behavior, options either remove default member initializers (thus making flag
aggregate), or provide constructor object::flag
.
Comments
Post a Comment