Suppose I have a class called generic_pair of the form:
template < typename K, typename V >
struct generic_pair{
K key;
V value;
};
Now, the problem is I would like to be able to store a bunch of these generic_pairs in an STL container BUT not all < K, V > in the container will be of the same type. For example, some elements may be < int, int > whereas others may be < int , string > and so on. Question is how can we do this?
My first thought is to use "tags" to create a hierarchy of encapsulated types and declare the container with the generic type but actual elements with inherited types. For example,
struct base_type{
typedef void type;
};
struct int_type: base_type{
typedef int type;
}
struct string_type: base_type{
typedef std::string type;
}
/// and so on establish a type hierarchy as necessary and then...
std::vector < generic_pair < base_type, base_type > > vec;
I bet there is a better, more correct way to do this? Any ideas, directions appreciated. If you have seen similar implementations or relevant tools/techniques in MPL or elsewhere that's helpful too. (I am trying to avoid macros)