Curious
Curious

Reputation: 21510

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?

Upvotes: 0

Views: 110

Answers (1)

Barry
Barry

Reputation: 303087

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.

Upvotes: 1

Related Questions