I have found several times that when a std::map is declared inside a class as a static inline (C++ 17),
struct MyStruct
{
static inline std::map <A, B> mymap;
MyStruct(A& a, B& b)
{
mymap[a] = b;
}
};
the MyStruct constructor will crash if it is called early, i.e. before main, inside the first use of the map member.
If the std::map is declared a different way, i.e.,
struct MyStruct
{
static std::map <A, B>& mymap()
{
static std::map <A, B> map;
return map;
}
MyStruct(A& a, B& b)
{
mymap()[a] = b;
}
};
then no crash happens.
I would have thought that in both cases the map would be initialized before the call to MyStruct constructor would be allowed to proceed.
Can anyone explain what is happening here?