xmllmx
xmllmx

Reputation: 42379

Why does C++ not allow variadic parameters in a non-template function?

int foo(int i)
{
    return i;
}

int foo(int i, int... n)
{
    return i + foo(n...);
}

int main()
{
    return foo(1, 2, 3); // error
}

Why does C++ not allow such an intuitive syntax?

Upvotes: 5

Views: 468

Answers (1)

Vir
Vir

Reputation: 459

You need the template mechanism to instantiate your second foo function, since the signature of the function is only determined when it's used. So the only feature you could ask for here, is that your syntax implies a function template where the template parameter pack is constrained to type int.

There is considerable opposition to templates that don't have the template keyword, though. However, things will change in that regard with the Concepts TS.

Upvotes: 6

Related Questions