简体   繁体   English

在C ++ 14中使用auto实现非类型模板参数

[英]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.

相关问题 C ++ 1y / C ++ 14:将静态constexpr数组转换为非类型模板参数包? - C++1y/C++14: Converting static constexpr array to non-type template parameter pack? 重新解释强制转换模板非类型参数:clang c ++ 14 vs c ++ 1z - Reinterpret cast a template non-type parameter: clang c++14 vs c++1z 使用constexpr数组作为模板非类型参数(C ++ 14) - Using constexpr array as a template non-type argument (C++14) 模板模板类中非类型参数的类型在 C++14 中不可推导,但在 C++17 中可推导 - Type of non-type parameter in template template class is non deducible in C++14 but deducible in C++17 C ++ 14是否定义类型为“ auto”的功能参数? - Does C++14 defines function parameters of type 'auto'? 在C ++ 14中使用auto作为返回和参数类型 - Use of auto as return and parameters type in C++14 非类型模板参数 - Non-type template parameters c ++ 17中的非类型模板参数可以是decltype(auto)吗? - Can non-type template parameters in c++17 be decltype(auto)? C ++ 17可以推导出具有显式非类型参数的`auto`非类型`模板`参数模式匹配模板吗? - Can C++17's deduced `auto` non-type `template` parameters pattern-match templates with explicit non-type parameters? 在 C++ 非类型模板参数中使用 decltype(auto) - Using decltype(auto) in C++ non-type template parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM