C++ 可变参数模板:根据可变参数模板大小列出所有数组元素的优雅方式
C++ variadic templates: elegant way of listing all array elements depending on variadic template size
是否有一些优雅的方式来表达函数调用取决于一些 n
可以派生自模板参数
f(my_array[0], ... , my_array[n-1]);
在看起来像这样的 class 模板中?:
template <int... numbers>
class Abraham {
static constexpr std::size_t n = sizeof...(numbers);
some_type my_array[n];
void foo(){
//...
f(my_array[0], ... , my_array[n-1]); // This line is no valid C++ Code. How can one achieve this in an elegant way?
//...
}
}
标准库有std::index_sequence
这种东西
template <size_t... Is>
void foo(std::index_sequence<Is...>) {
f(my_array[Is]...);
}
void foo() {
foo(std::make_index_sequence<n>{});
}
是否有一些优雅的方式来表达函数调用取决于一些 n
可以派生自模板参数
f(my_array[0], ... , my_array[n-1]);
在看起来像这样的 class 模板中?:
template <int... numbers>
class Abraham {
static constexpr std::size_t n = sizeof...(numbers);
some_type my_array[n];
void foo(){
//...
f(my_array[0], ... , my_array[n-1]); // This line is no valid C++ Code. How can one achieve this in an elegant way?
//...
}
}
标准库有std::index_sequence
这种东西
template <size_t... Is>
void foo(std::index_sequence<Is...>) {
f(my_array[Is]...);
}
void foo() {
foo(std::make_index_sequence<n>{});
}