模板参数扣除函数参数包后面跟其他参数 [英] Template arguments deduction for function parameter pack followed by other parameters

查看:137
本文介绍了模板参数扣除函数参数包后面跟其他参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

f1 f2 的扣减是否有欠妥之处?

  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 ++接受两者,clang只接受 f2 <



http://eel.is/c++draft/temp.deduct.call\"> [temp.deduct.call]


当函数参数包出现在非推导上下文([temp.deduct.type])中时,该参数包的类型从不推导。


[temp.deduct.type] p5


未推导出的上下文是:






因此,参数声明列表似乎MSVC在拒绝两个方面都是正确的?



这是否意味着模板的任何实例化都将是错误的,即使你明确指定了模板args? p>

  f1< int>(1,2); //形成错误? 
f2< int>(1,2); //形成错误?

如果是这样,为什么首先允许这样的声明?

解决方案

此特定问题有一个DR DR1388 。很明显,似乎GCC和CLANG尚未实施它 CLANG DR1388


这是否意味着模板的任何实例化将是
不成形,即使你明确指定了模板args?

  f1< int>(1,2); //形成错误? 
f2< int>(1,2); //形成错误?

如果是这样,为什么首先允许这样的声明?


如果您明确指定模板参数,则不会发生扣减,因此上述代码是合法的。


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?

解决方案

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.

这篇关于模板参数扣除函数参数包后面跟其他参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆