Search code examples
c++iteratorincrementvariadic-functionsparameter-pack

Incrementing every value in a parameter pack


I am currently attempting to increment every value in a parameter pack full of std::vector::iterators of some unknown type. I am currently struggling to get my head around how the ... syntax works. I would have thought to increment every value it would be ++input_starts ... but that just gives me a compiler error. Here is the entire function for reference:

template<
    typename RETURN,
    typename ... INPUTS
>
void thread_instance(std::function<RETURN(INPUTS ...)> function,
                     typename std::vector<RETURN>::iterator output_start,
                     typename std::vector<RETURN>::iterator output_end,
                     INPUTS ... input_starts)
{
    for (; output_start != output_end; ++output_start, ++input_starts ...)
    {
        *output_start = function(*input_starts ...);
    }
}

Solution

  • Replace this:

    ++input_starts ...
    

    With this:

    (++input_starts, ...)
    

    That is a C++17 fold expression (your use case is analogous to the push_back_vec() example on that page).

    Simple demo: https://godbolt.org/z/YoY4b1