Macro for template definitions

Multi tool use
Multi tool use

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Macro for template definitions



I have a problem with alias template, because I have a code which must be compatible with VS 2012, which does not support alias template.



Let's say I have an alias template like:


template<typename A, typename B> using foo = bar<A,B>;



Then it would be very convenient to be able to do something like:


#ifdef NO_ALIAS_TEMPLATE_AVAILABLE
#define foo<x,y> bar<x,y>
#else
template<typename A, typename B> using foo = bar<A,B>;
#endif



However the best I can do is


#define foo(x,y) bar<x,y>



And I don't want to replace all template specializations in all my code by round brackets, for code readability.



Is there any way to have a macro with delimiters <> for its argument? Or is there no simple solution to my problem? If not, how to implement a strict equivalence to alias template?


<>




2 Answers
2



No, the preprocessor cannot use <> to delimit macro arguments.


<>



You can emulate an alias template with a class template:


template <typename A, typename B>
struct foo
typedef bar<A, B> type;
;

// Usage: foo<A, B>::type
// Generic context: typename foo<A, B>::type



Demo



This would make its usage less nice, but that's unavoidable.



The following simpler solution works for me.


#ifdef NO_ALIAS_TEMPLATE_AVAILABLE
#define foo bar
#else
template<typename A, typename B> using foo = bar<A,B>;
#endif



Given


template <typename A, typename B> struct bar ;



the following lines work fine.


foo<int, double> f; // Ok.
bar<int, double> b; // Ok.





This is a terrible idea for common names of foo, but it's actually not that bad if you are careful with your macro names: #define MY_LIBRARY_FOO ::fully::qualified::bar and template <typename A, typename B> using MY_LIBRARY_FOO = bar<A, B>
– Justin
Aug 8 at 17:54


foo


#define MY_LIBRARY_FOO ::fully::qualified::bar


template <typename A, typename B> using MY_LIBRARY_FOO = bar<A, B>






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

q2j9l LZG1tRqBO9ea578MCG1Sl 6SYlO8 TCHgjaJsDrZpXP V7fHbAY75Go,wKi9cyQZ ieIPr VEzMfloonN205
kXG3,8WLFxx8,vk 8VGL,zp6 pr8r1,EnQp95x9U7P9sxl5SGo9E rftf9lc lPQU6WM,yG UdmE

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3