A minimal example follows:
#include <map>
#include <string>
static const std::map<std::string,int> myMap= {
{"a",1},{"b",2},{"c",3},{"d",4}
}
static const std::map<std::string,int> secondMap(myMap); /*(1)*/
int main(){
return 0;
}
When is the above map initialized?
My best guess is that it should be initialized before the call of main
, but I couldn't find relevant text in the standard.
Can myMap
be used by any other static variables without invoking undefined behavior? point /*(1)*/
demonstrates a possible usage.
Does the behavior change when the map is in a header file, instead of a translation unit?
I understand that there are different initialization phases for static variables in a C++ program, but the linked question does not answer which category the variable in the example belongs to.
My guess is that because it is constant, it would be static initialization, but honestly I'm not sure.