I have a few small header-only libraries (the header-only part is important). In the initial versions, I had some static members in the classes defined therein. It didn't occur to me until later (when I used them in a bigger project) that the static members would violate the ODR. I wanted to keep them header-only, so defining the static members in a separate .cpp file was out of the question. One well-known solution is to use a Meyers singleton function-local static variable for each static member (as suggested, for example, here).
That is all well and good, but since I want the singleton to behave like a member variable, I want to be able to get and set the value using setters and getters. But what do getters and setters for Meyers singletons function-local static variables look like? I haven't been able to find any solutions to that particular problem.
To clarify, these are the requirements:
- I want the behaviour of a static member variable inside a header-only library (so I cannot put definitions in a .cpp file)
- I want a getter which is a getter only (I shouldn't be able to modify the value by assigning to the reference returned by the getter)
- I also want to be able to modify the value via a dedicated setter.
EDIT 1:
I would like to explain why you might need this.
The static variables in the libraries I mentioned define the default values for some parameters. However, rather than hard-coding these default, I want to give the user the option to set the default values at the beginning of the program so they don't have to pass the values manually each time they call a member function or construct a new instance.
Also, although I agree that the use of the term "Meyers singleton" in the example provided here is misleading (I'm just using an int
value), there is nothing stopping you from using this paradigm with custom classes which you only want a single instance of. In such cases, the "Meyers singleton" term would be justified.
EDIT 2:
This has become somewhat irrelevant with the introduction of inline static
members in C++17, but I'll leave it up for people who don't have the option to use C++17.