Optional/ignored argument in C++ lambda, or lambda signature-based overload resolution

  c++, c++11, c++14

I have an iteration helper like this:

template <typename T, typename Op>
void for_each(T* a, size_t n, Op op) {
    for (size_t i = 0; i < n; i++) {
        a[i] = op(a[i], i);
    }
}

I’d like it to be able usable with or without the i argument to op:

// float* a;
for_each(a, n, [](const float& v) { return v + 1; });
for_each(a, n, [](const float& v, const size_t& i) { return v + i; });

Is there a way to do this with C++14 or earlier?

Note: I don’t have the option of using (const float& v, size_t) { } to ignore the second arg, nor the option of changing the op signature to std::pair<T, size_t> or similar.

Source: Windows Questions C++

LEAVE A COMMENT