Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template arguments deduction for function parameter pack followed by other parameters

Is the deduction for f1 and f2 ill-formed?

template<class... T, class U>
void f1(T..., U){}

template<class... T>
void f2(T..., int){}

int main()
{
    f1(1);
    f2(1);
    return 0;
}

g++ accepts both, clang only accepts f2, and msvc rejects both.

Related standard wording:

[temp.deduct.call]

When a function parameter pack appears in a non-deduced context ([temp.deduct.type]), the type of that parameter pack is never deduced.

[temp.deduct.type]p5

The non-deduced contexts are:

  • A function parameter pack that does not occur at the end of the parameter-declaration-list.

So it seems that MSVC is correct in rejecting both?

Does it mean that any instantiation of the templates will be ill-formed, even if you specify the template args explicitly?

f1<int>(1, 2); // ill-formed?
f2<int>(1, 2); // ill-formed?

If that's the case, why allow such declarations at first place?

like image 676
Jamboree Avatar asked Apr 15 '16 07:04

Jamboree


1 Answers

There's a DR for this specific issue DR1388. Aparently, it seems that GCC and CLANG haven't implemented it yet CLANG DR1388.

Does it mean that any instantiation of the templates will be ill-formed, even if you specify the template args explicitly?

f1<int>(1, 2); // ill-formed?
f2<int>(1, 2); // ill-formed?

If that's the case, why allow such declarations at first place?

No if you specify explicitly the template arguments, no deduction occurs and as such the code showed above is legal.

like image 184
101010 Avatar answered Nov 13 '22 02:11

101010