Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can X x(t...) ever result in a function declaration with vexing parse?

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?

like image 858
Johannes Schaub - litb Avatar asked Oct 23 '12 07:10

Johannes Schaub - litb


1 Answers

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.

like image 172
Xeo Avatar answered Oct 21 '22 23:10

Xeo