KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
After reading a lot of the questions regarding initialization of static variables I am still not sure how this applies to const variables at namespace level. I have kind of the following code in a header file config.h generated by the build script: static const std::string path1 = "/xyz/abc"; static const std::string path2 = "/etc"; According to what I have read the static keyword is not necessary, even deprecated here. My Question: Is the code above prone to the static initialization fiasco? If I have the following in a header file myclass.h : class MyClass { public: MyClass(const std::string& str) : m_str(str) {} std::string Get() const { return m_str; } private: std::string m_str; } const MyClass myclass1("test"); Will this pose any problems with static initialization? If I understood right, due to const variables having internal linkage there should be no problem in both cases? Edit: (due to dribeas answer) Maybe I should mention that I am interested in use cases like: In main.cpp : #include <config.h> #include <myclass.h> std::string anotherString(path1 + myclass1.Get()); int main() { ... } Another question regarding this use case: Will the compiler optimize away path2 in this case?
Tags (comma-separated)
Save Edits
Cancel