Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++, templatize T in std::pair <T, short>

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?

like image 835
justik Avatar asked Nov 27 '25 19:11

justik


1 Answers

It sounds like you want a "template alias" which apparently was added to the standard with C++11. The syntax in your case would be something like:

template <typename T>
using TPair = std::pair<T,short>;

template <typename T>
using TPairs = std::vector<TPair<T>>;

[Disclaimer: I haven't tried this, so it may be nonsense.]

like image 175
Oliver Charlesworth Avatar answered Nov 29 '25 08:11

Oliver Charlesworth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!