Search code examples
c++parameter-pack

Parameter pack extraction


I have a function2, which can be called with or without a second argument == char. If so, I want to modify that char-Argument.

Given

void function1_caller(int x) { 
    char ws=7; 
    function2_modifyArg(x, ws); 
}

This works:

template <typename ... WS> 
void function2_modifyArg(int x, WS ... ws) { 
    function3_end(x, ws ...);
}

simply put in parenthesis, doesn't work already:

template <typename ... WS> 
void function2_modifyArg(int x, WS ... ws) { 
    function3_end(x, (ws ... )); 
}

This results in the following error:

Syntax error: unexpected token '...', expected declaration

In fact here I'd like to modify the argument, but of course I get the same

Syntax error: unexpected token '...', expected declaration
template <typename ... WS> 
void function2_modifyArg(int x, WS ... ws) { 
    function3_end(x, (ws ... / 3));
}

template <typename ... WS> 
void function3_end(int x, WS ... ws) {};

Solution

  • You don't need the parenthesis you just need to put the operation you want to do before the pack expansion:

    template <typename ... WS> 
    void function2_modifyArg(int x, WS ... ws) { 
        function3_end(x, ws / 3 ...);
    }