绑定可变函数 C++

Binding Variadic Functions C++

假设我有这样一个函数:

template <typename... T>
void sum(int start, T... next);

我想绑定这个。我该怎么做?


更准确地说,如果函数有固定数量的参数,它看起来像:

void sum(int start, int next); // function signature

// ...

auto binded_sum = std::bind(sum, 0, std::placeholders::_1);

现在我如何 'placehold' 可变数量的参数?

大致如下:

auto binded_sum = [](auto... next) { sum(0, next...); };

值得一提的是,C++20 也为此目的提供了 std::bind_front

auto bound_sum = std::bind_front(sum, 0);

(与简单的 lambda 实现相比,这有许多潜在的优势;如果您如此担心,请参阅 paper proposing the feature 了解详细信息。)