C++ 在可变模板中构造固定大小数组
我想在一个变量函数中构造一个由8个整数组成的数组。没问题:C++ 在可变模板中构造固定大小数组,c++,c++11,variadic-templates,C++,C++11,Variadic Templates,我想在一个变量函数中构造一个由8个整数组成的数组。没问题: template <typename... Ints> void foo(Ints... ints) { static_assert(sizeof...(Ints) < 8, "some useful error"); const int my_array[8] = {ints...}; } 但是有没有一种更简单的方法不依赖于这个额外的类型呢?只需使用另一个模板: template <type
template <typename... Ints>
void foo(Ints... ints) {
static_assert(sizeof...(Ints) < 8, "some useful error");
const int my_array[8] = {ints...};
}
但是有没有一种更简单的方法不依赖于这个额外的类型呢?只需使用另一个模板:
template <typename... Ints>
auto foo(Ints... ints) -> typename std::enable_if<sizeof...ints==8>::type {
const int my_array[] = {ints...};
}
template <typename... Ints>
auto foo(Ints... ints) -> typename std::enable_if<sizeof...ints<8>::type {
return foo(ints..., -1);
}
模板
自动foo(Ints…Ints)->typename std::enable\u if::type{
常量int my_数组[]={int…};
}
模板
自动foo(Ints…Ints)->typename std::enable\u if
template <typename... Ints>
auto foo(Ints... ints) -> typename std::enable_if<sizeof...ints==8>::type {
const int my_array[] = {ints...};
}
template <typename... Ints>
auto foo(Ints... ints) -> typename std::enable_if<sizeof...ints<8>::type {
return foo(ints..., -1);
}