我正在尝试实现以下功能(使用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>
激活吗?
答案 0 :(得分:9)
1,2和3不是类型。您的专业化不会(也不能)将主模板更改为接受值,因此您无法神奇地传递之前预期类型的值。
如果您想要一个接受值的模板,别名模板可以这样做而不是专业化:
template<auto... values_>
using VTuple = Tuple<constant_t<values_>... >;
但它是单独的模板。