简体   繁体   中英

Achieving non-type template parameters with auto in C++14

Is there a way one can mimic the auto deduction in non type template parameters in C++14? Similar to how you can mimic unconstrained arguments in C++14 lambdas in C++11 with templated functors?

Sort of. You can have non-type template parameters of course, but you need to specify the type. The common idiom for that is:

template <class T, T Value>
struct X;

But you can't instantiate something like X<3> with it. The best you can do is introduce a macro to pull out the type for you:

#define DECL(expr) decltype(expr), (expr)
X<DECL(3)> x;

Which for 3 is obviously silly, but does help a bit when you want to provide something like a function pointer as a non-type template argument.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM