15

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?

4

1 回答 1

13

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.

于 2012-10-23T07:52:37.893 回答