[英]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? 有没有办法可以模仿C ++ 14 中非类型模板参数的自动推导 ? Similar to how you can mimic unconstrained arguments in C++14 lambdas in C++11 with templated functors?
类似于如何使用模板化函子模拟C ++ 11中C ++ 14 lambda中的无约束参数?
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. 但是您不能使用它实例化诸如
X<3>
的东西。 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. 对于
3
来说,这显然是愚蠢的,但是当您想要提供诸如函数指针之类的非类型模板参数时,它确实有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.