First the apologies, i’m not sure if my question title even accuratly explains what I’m asking – I’ve had a look through google, but i’m not sure which terms I need in my search query, so the answer may be out there (or even on StackOverflow) already.
I have a templated class, which basically looks like the following – it uses the Singleton pattern, hence everything is static, I’m not looking for comments on why I’m storing the keys in a set and using strings etc, unless it actually provides a solution. There’s a bit more to the class, but that isn’t relevant to the question.
template<typename T>
class MyClass
{
private:
//Constructor and other bits and peices you don't need to know about
static std::set<std::string> StoredKeys;
public:
static bool GetValue(T &Value, std::string &Key)
{
//implementation
}
static SetValue(const T Value, std::string &Key)
{
//implementation
StoredKeys.Insert(Key);
}
static KeyList GetKeys()
{
return KeyList(StoredKeys);
}
};
Later on in some other part of the application I want to get all the Keys for all of the values – regardless of type.
Whilst I am fairly confident that at the moment only 3 or 4 types are being used with the class so I could write something like:
KeyList Keys = MyClass<bool>::GetKeys();
Keys += MyClass<double>::GetKeys();
Keys += MyClass<char>::GetKeys();
This will need to be updated each time a new type is used. It also has the downside of instantiating the class if it’s not used anywhere.
I think (again I could be wrong) that metaprogramming is the answer here, some sort of macro maybe?
We’re using boost, so I’m guessing the MPL library could be useful here?
This aspect of STL is a bit new to me, so I’m happy to read up and learn as much as I need, just as soon as I know exactly what it is I need to learn to engineer a solution.
Move
StoredKeysinto a non-template base classclass MyClassBase, or add anAllStoredKeysstatic member to a non-template base class.Alternatively, create a static init method called from
SetValuethat adds a pointer toStoredKeysto a static list.