Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ...);
    }
}
like image 568
finlay morrison Avatar asked May 15 '26 15:05

finlay morrison


1 Answers

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

like image 187
John Zwinck Avatar answered May 17 '26 07:05

John Zwinck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!