Pack expansion in variadic templates when ellipsis are on innermost element of a pattern

c++, c++11, templates, variadic-templates

Solution

This works:

template<typename ...Ts, int... N> void g(Ts (&...array)[N]) {}
int n[1];
g<const char, int>("a", n); // Ts (&...)[N] expands to const char (&)[2], int(&)[1]

but apparently many compilers have a problem if you skip the name `array`. I don't know if this is a quirk of the compilers or of the standard (both are reasonable: this is a serious bit of corner-case syntax).

As noted by @Xeo, a less obtuse way to deal with the declaration syntax of C is to cheat our way around the issue:

template<typename T> using Type = T;
template<typename... Ts, int... N> void g( Type<Ts[N]>&... ) {}

is easier to parse and removes the need for (at least some) compilers to have a variable name.

Problem

I saw this example on cppreference.com. I am not clear on the pack expansion of the function arguments. Function parameter list In a function parameter list, if an ellipsis appears in a parameter declaration (whether it names a function parameter pack (as in, Args ... args) or not) the parameter declaration is the pattern: ``` template<typename ...Ts> void f(Ts...) {} f('a', 1); // Ts... expands to void f(char, int) f(0.1); // Ts... expands to void f(double) template<typename ...Ts, int... N> void g(Ts (&...)[N]) {} int n[1]; g<const char, int>("a", n); // Ts (&...)[N] expands to const char (&)[2], int(&)[1] ``` Note: in this pattern, the ellipsis is the innermost element, not the last element as in all other pack expansions.

Original source