C ++ 20中用户定义的推导指南 [英] User-Defined Deduction Guides in C++20

查看:64
本文介绍了C ++ 20中用户定义的推导指南的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用在cppreference上,有一个一个示例,其中说:

On cppreference, there's an example that says:

// explicit deduction guide (not needed as of C++20)
template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;

为什么在C ++ 20中不再需要此推论指南?删除类型推导指南会导致编译在所有当前编译器上均失败,但是没有人完全支持C +现在为+20,所以这没有任何意义.谁能指出我讨论该规范的那一部分?

Why is this deduction guide no longer needed in C++20? Removing the type deduction guide causes the compile to fail on all current compilers, but no one fully supports C++20 right now so that doesn't mean anything. Can anyone point me to the part of the spec that discusses this?

推荐答案

此更改是由 P1021R4 添加的,特别是 P1816R0 ,它增加了对聚合的类模板参数推论的支持.

This change was added by P1021R4, specifically P1816R0, which added support for class template argument deduction for aggregates.

可以在 [over.match.class]中找到特定的措辞.deduct]/1 ,基本上为聚合添加了一个隐式推导指南:

The specific wording can be found in [over.match.class.deduct]/1 which adds, basically, an implicit deduction guide for aggregates:

此外,如果定义了 C ,并且其定义满足聚合类([dcl.init.aggr])的条件,则假定任何从属基类都没有虚函数且没有虚拟基类,并且初始化程序是一个非空的 braised-init-list 带括号的表达式列表,并且没有推导指南对于 C ,该集合包含一个附加的功能模板,称为汇总扣除候选者,定义如下.令x1,…,xn是 braised-init-list initializer-list 的元素,或 expression-list 的名称.对于每个xi,令ei为 C 或其(可能是递归)子聚合之一的相应聚合元素,如果

In addition, if C is defined and its definition satisfies the conditions for an aggregate class ([dcl.init.aggr]) with the assumption that any dependent base class has no virtual functions and no virtual base classes, and the initializer is a non-empty braced-init-list or parenthesized expression-list, and there are no deduction-guides for C, the set contains an additional function template, called the aggregate deduction candidate, defined as follows. Let x1,…,xn be the elements of the initializer-list or designated-initializer-list of the braced-init-list, or of the expression-list. For each xi, let ei be the corresponding aggregate element of C or of one of its (possibly recursive) subaggregates that would be initialized by xi ([dcl.init.aggr]) if

  • 对于任何具有依赖的非数组类型或具有值依赖的绑定的数组类型的聚合元素,均不考虑括号省略,并且
  • 假设
  • 作为包扩展的每个非跟踪聚合元素都不对应于初始化列表的任何元素,并且
  • 假定为包扩展的尾随聚合元素与初始值设定项列表的所有其余元素(如果有)相对应.

如果对于任何xi没有这样的合计元素ei,则合计推算候选项不会添加到集合中.汇总扣除候选者是从假设构造函数C(T1,…,Tn)的上面导出的,其中

If there is no such aggregate element ei for any xi, the aggregate deduction candidate is not added to the set. The aggregate deduction candidate is derived as above from a hypothetical constructor C(T1,…,Tn), where

  • 如果ei是数组类型,并且xi是大括号初始化列表或字符串文字,则Ti是对ei的声明类型的右值引用,并且
  • 否则,Ti是ei的声明类型,

只是将形式为Pj ...的其他参数包以其原始集合元素位置插入参数列表中,该位置对应于由于是参数包而被跳过的,类型为Pj的每个非跟踪集合元素,并且对应于作为包扩展(如果有)的尾随聚合元素的尾随参数序列将替换为Tn ...形式的单个参数.

except that additional parameter packs of the form Pj... are inserted into the parameter list in their original aggregate element position corresponding to each non-trailing aggregate element of type Pj that was skipped because it was a parameter pack, and the trailing sequence of parameters corresponding to a trailing aggregate element that is a pack expansion (if any) is replaced by a single parameter of the form Tn....

基本上,是您之前必须写的演绎指南:

Basically, the deduction-guide that you had to write before:

template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;

现在不属于该语言.

这篇关于C ++ 20中用户定义的推导指南的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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