Is it possible to declare and define a const static instance of a class in the class header file.
I want to do something like this (from this similar question):
class PlaceID {
public:
inline PlaceID(const std::string placeName):mPlaceName(placeName) {}
const static PlaceID OUTSIDE;
private:
std::string mPlaceName;
};
const PlaceID PlaceID::OUTSIDE = PlaceID("");
This would work if the definition of PlaceID::OUTSIDE was in a source file, but if it's in a header file that is include in multiple location it causes an link error because PlaceID::OUTSIDE is then defined multiple times.
I'd like to define it in the header file for two reasons. First, this will be part of a library and I'd like the library to be header file only.
Second and this is the most important one I want the compiler to be allowed to "inline" the uses of this instance. The class in question (not the one used as an example here) is a wrapper around a primitive type with all methods inlined in order to offer the same performance as the primitive type would. If I place the definition of this instance in a source file, the compiler will not know it's value at compilation time and won't be able to apply some optimisations.
Thanks.