I was writing a function template as
template<typename ...T>
void f(T ...t) {
X x(t...);
// ...
}
When I was looking at it, I was wondering what happens for a call f()
. Will vexing parse make x a function declaration? Compilers seem to make it a variable. Can someone help me be sure about it please?
The empty parenthesis (()
) will make it a function declaration only if it's written as such in the source code.
§14.5.3 [temp.variadic] p6
also mentions this:
The instantiation of a pack expansion that is not a sizeof... expression produces a list E1, E2, ..., EN, where N is the number of elements in the pack expansion parameters. [...] When N is zero, the instantiation of the expansion produces an empty list. Such an instantiation does not alter the syntactic interpretation of the enclosing construct, even in cases where omitting the list entirely would otherwise be ill-formed or would result in an ambiguity in the grammar. [ Example:
template<class... T> struct X : T... { }; template<class... T> void f(T... values) { X<T...> x(values...); } template void f<>(); // OK: X<> has no base classes // x is a variable of type X<> that is value-initialized
—end example ]
See specifically the second comment in the example code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With