Fle*_*ght 1 c++ tuples type-traits c++17
我试图实现以下(使用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;
}
Run Code Online (Sandbox Code Playgroud)
这给了我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’
Run Code Online (Sandbox Code Playgroud)
任何人都可以解释为什么部分特化不会激活Tuple<1, 2, 3>?
1,2和3不是类型.您的专业化不会(也不能)将主模板更改为接受值,因此您无法神奇地传递之前预期类型的值.
如果您想要一个接受值的模板,别名模板可以执行该操作而不是专门化:
template<auto... values_>
using VTuple = Tuple<constant_t<values_>... >;
Run Code Online (Sandbox Code Playgroud)
但它是一个单独的模板.