I'm trying to use std::hash_map
to define a nested object. I'm using Visual Studio 2013.
The trouble starts when I try to use nested initialization literals. I've reduced my approach to the following:
enum ENUM1 {
ENUM1_A,
ENUM1_B,
};
enum ENUM2 {
ENUM2_A,
ENUM2_B
};
enum ENUM3 {
ENUM3_A,
ENUM3_B
};
std::hash_map<ENUM1, std::hash_map<ENUM2, std::hash_map<ENUM3, int>>> A = {
{
ENUM1_A, {
{
ENUM2_A, {
{ ENUM3_A, 123 },
{ ENUM3_B, 45 },
},
},
{
ENUM2_B, {
{ ENUM3_A, 733 },
{ ENUM3_B, 413 },
}
}
}
}
};
The compiler doesn't complain, but on execution it results in an access violation and the following call stack:
***.exe!std::list<std::pair<enum ENUM3 const ,int>,std::allocator<std::pair<enum ENUM3 const ,int> > >::clear() Line 1503 C++
***.exe!std::list<std::pair<enum ENUM3 const ,int>,std::allocator<std::pair<enum ENUM3 const ,int> > >::_Tidy() Line 1884 C++
***.exe!std::list<std::pair<enum ENUM3 const ,int>,std::allocator<std::pair<enum ENUM3 const ,int> > >::~list<std::pair<enum ENUM3 const ,int>,std::allocator<std::pair<enum ENUM3 const ,int> > >() Line 1096 C++
***.exe!std::_Hash<stdext::_Hmap_traits<enum ENUM3,int,stdext::hash_compare<enum ENUM3,std::less<enum ENUM3> >,std::allocator<std::pair<enum ENUM3 const ,int> >,0> >::~_Hash<stdext::_Hmap_traits<enum ENUM3,int,stdext::hash_compare<enum ENUM3,std::less<enum ENUM3> >,std::allocator<std::pair<enum ENUM3 const ,int> >,0> >() Line 409 C++
***.exe!stdext::hash_map<enum ENUM3,int,stdext::hash_compare<enum ENUM3,std::less<enum ENUM3> >,std::allocator<std::pair<enum ENUM3 const ,int> > >::~hash_map<enum ENUM3,int,stdext::hash_compare<enum ENUM3,std::less<enum ENUM3> >,std::allocator<std::pair<enum ENUM3 const ,int> > >() C++
***.exe!std::pair<enum ENUM2 const ,stdext::hash_map<enum ENUM3,int,stdext::hash_compare<enum ENUM3,std::less<enum ENUM3> >,std::allocator<std::pair<enum ENUM3 const ,int> > > >::~pair<enum ENUM2 const ,stdext::hash_map<enum ENUM3,int,stdext::hash_compare<enum ENUM3,std::less<enum ENUM3> >,std::allocator<std::pair<enum ENUM3 const ,int> > > >() C++
***.exe!`dynamic initializer for 'A''() Line 76 C++
Unfortunately I'm very new to C++ and don't know what to make of it myself.
I've seen the following question where the answer references a compiler bug, but the linked bug report specifically mentions that this problem is related to string literals.
What am I doing wrong? Somewhat unrelated, what prevents an object with a constant value like this from being calculated at compile time? Is there a better way to define JSON-like objects in the code?