How to define static const std::string class variable which can be used everywhere in my program safely?
1st approach - fails static initialization order fiasco:
file: Consts.h
namespace constants {
struct Consts {
static const std::string kVar = "123";
}
}
2nd approach - leads to copying kVar into every translation unit we are including this header to, which leads to One Definition Rule violation principle and may cause double free or use after free errors - this is undefined behaviour if this definition is included into multiple cpp files (which I want to do because I want global shared std::string const).
file: Consts.h
namespace constants {
const std::string kVar = "123";
}
Is there better way (except using macros - which is global so ugly solution as well) to define such var in safe way? What are the best proven practises to such constructs?