I would like to templatize “first” type of std::pair using the following construction
template <typename T>
struct TPair
{
typedef std::pair <T, short> Type;
};
and create a vector of such pairs.
template <typename T>
struct TPairs
{
typedef std::vector <TPair <T> > Type;
};
But this code seems to be screwed for common usage and it is uncomfortable:
TPair <double> ::Type my_pair (1.0, 0 ); //Create pairs
TPair <double> my_pair2 (1.0, 0 ); //Create object, needs a constructor
TPairs <double> ::Type pairs; //Create vector
TPairs <double> pairs2; //Create object
pairs.push_back(my_pair); //Need a constructor
pairs2.push_back(my_pair); //No push_back method for the structure...
....
Is there any more simple and comfortable solution?
There’s a problem here: you’re creating a type that is a vector of
TPair<T>, which is in fact not what you want. You want a vector ofTPair<T>::Type.As for your use cases, remember that those two structs you created are there just to simulate a template typedef, you should never instantiate them at all, just use their
Typemember typedef. So: