是否有一些优雅的方式来表达函数调用,具体取决于可以从模板参数派生的一些n
f(my_array[0], ... , my_array[n-1]);
在类模板中看起来像这样?:
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>{});
}
关于C++ 可变参数模板 : elegant way of listing all array elements depending on variadic template size,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64952858/