3

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 ...);
    }
}
finlay morrison
  • 225
  • 1
  • 5
  • The same situation as this question, but with `operator++` instead of `some_function`: https://stackoverflow.com/questions/16011873/c11-variadic-templates-and-comma-separated-expressions-equivalence . (That question's answers also work prior to C++17) – M.M Oct 17 '20 at 01:40

1 Answers1

3

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

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • thanks for the simple solution! also, I was wondering if its possible to do somthing similar to that but incremend every iterator by another variable, my code seems to not work as i would expect it to: ```cpp threads[i] = std::thread(thread_instance, function, thread_output_start, thread_output_end, (input_vectors.begin() + start_index, ...)); ``` – finlay morrison Oct 17 '20 at 01:40
  • 1
    @finlaymorrison: For that I think you need `(input_vectors.begin() + start_index), ...` (you had the ellipsis inside the parentheses, which is wrong). – John Zwinck Oct 17 '20 at 03:25