amigo421 amigo421 - 21 days ago 5
C++ Question

expansion parameters pack (values) from template arguments

there is a trying to expand the pack using usual way with recursion:

template<bool first> int func1(int value = 0) {
return some_func(first, value);
}

template<bool first, bool... args> int func1(int value = 0) {
return func1<args...>(some_func(first, value) );
}


at the last step of compile time recursion, the call of func1 is ambiguous,
first candidate is a first function , it's clear , some concrete specialization in my case:


int func1(int) [with bool first = false]


but second one is


int func1(int) [with bool first = false; bool ...args = {}]


you see that is also correct - empty set of the arguments after first one.
any idea to prevent this ?

thank you

Answer

Disambiguate the base case from the recursive case by adding an explicit second parameter:

template<bool first> int func1(int value = 0) {
    return some_func(first, value);
}

template<bool first, bool second, bool... args> int func1(int value = 0) {
    return func1<second, args...>(some_func(first, value) );
}

Wandbox example