作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试实现以下目标(使用 C++17 功能):
#include <type_traits>
template<auto value_>
using constant_t = std::integral_constant<decltype(value_), value_>;
template<typename ... > class Tuple {};
template<auto ... values_>
class Tuple<constant_t<values_> ... > {};
int main(void)
{
Tuple<int, int, char> types;
Tuple<1, 2, 3> values;
}
这让我在 g++-7.1.0 中出现以下错误
main.cpp: In function ‘int main()’:
main.cpp:15:18: error: type/value mismatch at argument 1 in template parameter list for ‘template<class ...> class Tuple’
Tuple<1, 2, 3> values;
^
main.cpp:15:18: note: expected a type, got ‘1’
main.cpp:15:18: error: type/value mismatch at argument 1 in template parameter list for ‘template<class ...> class Tuple’
main.cpp:15:18: note: expected a type, got ‘2’
main.cpp:15:18: error: type/value mismatch at argument 1 in template parameter list for ‘template<class ...> class Tuple’
main.cpp:15:18: note: expected a type, got ‘3’
谁能解释为什么偏特化没有为 Tuple<1, 2, 3>
激活? ?
最佳答案
1、2 和 3 不是类型。您的特化不会(也不能)更改主模板以接受值,因此您无法神奇地将值传递到之前需要类型的地方。
如果你想要一个接受值的模板,别名模板可以代替特化:
template<auto... values_>
using VTuple = Tuple<constant_t<values_>... >;
但它是一个单独的模板。
关于c++ - 为什么不采用非类型偏特化元组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44897951/
我是一名优秀的程序员,十分优秀!