Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在可变模板中构造固定大小数组_C++_C++11_Variadic Templates - Fatal编程技术网

C++ 在可变模板中构造固定大小数组

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

我想在一个变量函数中构造一个由8个整数组成的数组。没问题:

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);
}