I was looking at \"How to properly use references with variadic templates,\" and wondered how far comma expansion can go.
Here\'s a variant of the answer:
Unpacking is only allowed in certain contexts, and comma separated statements doesn't belong to them. Using your words: The expansion is semantically and not lexically. However, it doesn't matter because there are several other ways of doing it. There are already some kind of patterns/idioms to write simple variadic functions. One way of doing it:
Use a helper template function, that does nothing at all:
template <typename ...Args>
void pass(Args&&...) { }
Instead of using the comma operator, pass the expressions to this function:
template <typename ...Args>
void inc(Args&&... args)
{
pass(++std::forward<Args>(args)...);
}
You can use the comma operator within the expansion, if the expressions have to be more complex. This might be useful in your case, if some operator++
have return type void
:
pass((++std::forward<Args>(args), 0)...);