Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: generate template argument?

I run into the unfortunate situation that I need to write the following code:

std::map<type1_key, type1_value, type1_cmp> x;
std::map<type2_key, type2_value, type2_cmp> y;
std::map<type3_key, type3_value, type3_cmp> z;
// ...

These key/value/cmp types are predefined for me. Their type names all follow this _key, _value, and _cmp pattern (those are not templates). Is there a built-in way to simplify this into

map_type<type1> x;
// ...

or

map_type(type1) x; // some kind of macro??
// ...
like image 368
MetroWind Avatar asked Jan 25 '26 16:01

MetroWind


1 Answers

You can use following macro:

   #define map_type( x ) std::map<x##_key, x##_value, x##_cmp>

Then,

map_type(type1) x;
like image 186
P0W Avatar answered Jan 27 '26 05:01

P0W



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!