2

I am implementing a new header-only Library in VS2019, targeting C++11. As discussed (How to initialize a static const member in C++?) you cannot initialize a non-int static class member in-line.

class A { private: static const std::string SPECIAL_KEY = "A8787HHH"; //NOT OK /*...*/ }; 

Instead you have to define it outside the class, typically in a CPP file:

class A { private: static const std::string SPECIAL_KEY; /*...*/ }; const std::string A::SPECIAL_KEY = "A8787HHH"; 

But in a header-only library this runs into problems. I tried to do the above all in the same header and it worked in a simple test-app, but in real use I got linker errors that symbol A::SPECIAL_KEY was multiply defined, not surprising really as the header will get included in multiple places (How to initialize private static members in C++?)

In C++17 you can define in-line (https://stackoverflow.com/a/45062055/197229) but that's not any help to me on this code-base. C++ doesn't provide static class constructors either.

Header-only libraries have become quite widely used so is there a standard workaround to what must be a common sort of problem, until such time as we might be able to upgrade to C++17?

0

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.