gpt4 book ai didi

c++ - 为什么不采用非类型偏特化元组?

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:25:00 25 4
gpt4 key购买 nike

我正在尝试实现以下目标(使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com