简体   繁体   English

什么是通用参考的标准/官方名称?

[英]What's the standard/official name for universal references?

I know that if a variable or parameter is declared to have type T&& for some deduced type T , that variable or parameter is widely called a universal reference . 我知道如果一个变量或参数被声明为某个推导类型T类型为T&& ,那么该变量或参数被广泛称为通用引用

The term universal reference was introduced by Scott Meyers in his original talk “Universal References in C++11” . Scott Meyers在其最初的演讲“C ++ 11中的通用参考”中引入了通用参考这一术语。 However, I wonder what's the official/standard term for universal references . 但是,我想知道普遍参考的官方/标准术语是什么。

Overview 概观

It is known that since C++11, a parameter of type T&& is called an rvalue reference [ ISO/IEC 14882:2011 §8.3.2/p2 References [dcl.ref] ]. 众所周知,从C ++ 11开始, T&&类型的参数称为右值参考 [ ISO / IEC 14882:2011§8.3.2/ p2 References [dcl.ref] ]。 That is, unless T is a template parameter type or auto or a typedef for some lvalue reference type. 也就是说,除非T是模板参数类型或auto或某个左值引用类型的typedef

examples: 例子:

 template<typename T> void foo(T&& p) { // -> T is a template parameter ... } auto &&p = expression; 

Although technically T&& in the examples above is still an rvalue reference, its behaviour differs significantly from a regular one. 虽然上面的例子中技术上T&&仍然是右值参考,但它的行为与常规参数有很大不同。

Naturally, you would ask "why this special case doesn't have a special syntax". 当然,你会问“为什么这个特殊情况没有特殊的语法”。 The answer is that the && syntax was intentionally overloaded for this special construct by the C++ committee. 答案是C ++委员会为这个特殊的构造故意重载了&&语法。 However, they missed to name this special case. 然而,他们错过了这个特例的名称。

In the absence of a distinct name for this particular construct, Scott Meyers coined the widely known term/name universal references . 由于这个特定结构没有明确的名称, Scott Meyers创造了广为人知的术语/名称通用参考文献

The committee however, decided that this name is not proper for a number of reasons. 但委员会认为,由于多种原因,这个名称并不合适。 As such, the proposal N4164 made by Herb Sutter , Bjarne Stroustrup and Gabriel Dos Reis proposed to change the name to Forwarding References . 因此, Herb SutterBjarne StroustrupGabriel Dos Reis提出的N4164提议将名称改为Forwarding References

The name Forwarding References had the most support in informal discussions among committee members, including the authors of the proposal mentioned earlier. 转发参考文献的名称在委员会成员之间的非正式讨论中得到了最多的支持,包括前面提到的提案的作者。 Interestingly enough, it was Scott Meyers himself that introduced that term in his original “Universal References” talk. 有趣的是,Scott Meyers本人在他最初的“Universal References”演讲中引入了这个词。 However, later he decided to go along with the name universal references . 然而,后来他决定继续使用名称通用引用 For this decision played role the fact that at the time he didn't think that the term forwarding references included also the auto&& case. 因为这个决定扮演了一个角色,当时他并不认为转发引用一词也包括auto&&案例。

Why not universal references ? 为什么不普遍参考

According to the proposal the term Universal references although is a reasonable name with an obvious meaning, it happens to be wrong in several aspects. 根据该提议,术语“ 通用引用”虽然是一个具有明显含义的合理名称,但它在几个方面恰好是错误的。

A universal reference must mean the following: 通用引用必须表示以下内容:

  • A reference that can be used everywhere; 可以在任何地方使用的参考; or 要么
  • A reference that can be used for everything; 可用于所有内容的引用; or 要么
  • something similar. 相似的东西。

Obviously this is not the case nor is the appropriate use of this construct. 显然情况并非如此,也不适合使用这种结构。 Furthermore, this name would encourage many people to consider that something having such a name is meant to be used "universally". 此外,这个名称会鼓励许多人认为具有这种名称的东西意味着“普遍”使用。 Something that the committee considered it a bad thing. 委员会认为这是件坏事。

Moreover, "universal references" aren't even really references per se, but rather a set of rules for using references in a particular way in a particular context with some language support for that use, and that use is forwarding . 此外,“通用引用”本身甚至不是真正的引用,而是一组规则,用于在特定上下文中以特定方式使用引用,并为该用途提供某些语言支持,并且该用法是转发

Why auto&& is Also Considered a Forwarding case 为什么auto&&也被认为是一个转发案例

auto&& is also considered a forward case since it follows the reference collapsing rules . auto&&也被视为前瞻性案例,因为它遵循参考折叠规则 For example in: 例如:

  • Generic lambdas of the form, [](auto&& x){ … } 形式的通用lambda, [](auto&& x){ … }
  • for -ranged loop of the form, for(auto &&i : v) { ... } for -ranged循环的形式, for(auto &&i : v) { ... }
  • Finally, in general is true that auto&& local variables are for forwarding. 最后,一般情况下auto&&局部变量用于转发。

Standard Wordings for Forwarding References 转发参考的标准措辞

The term forwarding references is mentioned in the draft standard N4527 in the following places: 术语转发参考在以下地方的标准草案N4527中提到:

§14.8.2.1/ Deducing template arguments from a function call [temp.deduct.call] (Emphasis Mine) : §14.8.2.1/从函数调用中减去模板参数[temp.deduct.call] (Emphasis Mine)

If P is a cv-qualified type, the top level cv-qualifiers of P's type are ignored for type deduction. 如果P是cv限定类型,则类型推导将忽略P类型的顶级cv限定符。 If P is a reference type, the type referred to by P is used for type deduction. 如果P是引用类型,则P引用的类型用于类型推导。 A forwarding reference is an rvalue reference to a cv-unqualified template parameter. 转发引用是对cv-unqualified模板参数的rvalue引用。 If P is a forwarding reference and the argument is an lvalue, the type “lvalue reference to A” is used in place of A for type deduction. 如果P是转发引用且参数是左值,则使用类型“对A的左值引用”代替A来进行类型推导。 [ Example: [例如:

 template <class T> int f(T&& heisenreference); template <class T> int g(const T&&); int i; int n1 = f(i); // calls f<int&>(int&) int n2 = f(0); // calls f<int>(int&&) int n3 = g(i); // error: would call g<int>(const int&&), which // would bind an rvalue reference to an lvalue 

— end example ] - 结束例子]

§14.8.2.5/p10 Deducing template arguments from a type [temp.deduct.type]: §14.8.2.5/ p10从类型[temp.deduct.type]中推导出模板参数:

Similarly, if P has a form that contains (T), then each parameter type Pi of the respective parameter-typelist of P is compared with the corresponding parameter type Ai of the corresponding parameter-type-list of A. If P and A are function types that originated from deduction when taking the address of a function template (14.8.2.2) or when deducing template arguments from a function declaration (14.8.2.6) and Pi and Ai are parameters of the top-level parameter-type-list of P and A, respectively, Pi is adjusted if it is a forwarding reference (14.8.2.1) and Ai is an lvalue reference, in which case the type of Pi is changed to be the template parameter type (ie, T&& is changed to simply T). 类似地,如果P具有包含(T)的形式,则将P的各个参数类型列表的每个参数类型Pi与相应的参数类型列表A的相应参数类型Ai进行比较。如果P和A是当获取函数模板的地址(14.8.2.2)或从函数声明(14.8.2.6)中推导出模板参数时,源自演绎的函数类型以及Pi和Ai是顶级参数类型列表的参数如果它是转发参考 (14.8.2.1)并且Ai是左值参考,则P和A分别被调整,在这种情况下,Pi的类型被改变为模板参数类型(即,T &&被简单地改变为T)。 [ Note: As a result, when Pi is T&& and Ai is X& , the adjusted Pi will be T, causing T to be deduced as X& . [注意:结果,当Pi为T&&且Ai为X& ,调整后的Pi将为T,导致T被推导为X& — end note ] [Example: - 尾注] [示例:

 template <class T> void f(T&&); template <> void f(int&) { } // #1 template <> void f(int&&) { } // #2 void g(int i) { f(i); // calls f<int&>(int&), ie, #1 f(0); // calls f<int>(int&&), ie, #2 } 

— end example ] If the parameter-declaration corresponding to Pi is a function parameter pack, then the type of its declaratorid is compared with each remaining parameter type in the parameter-type-list of A. Each comparison deduces template arguments for subsequent positions in the template parameter packs expanded by the function parameter pack. - 结束示例]如果对应于Pi的参数声明是函数参数包,则将其declaratorid的类型与A的参数类型列表中的每个剩余参数类型进行比较。每个比较推导出后续位置的模板参数。由函数参数包扩展的模板参数包。 During partial ordering (14.8.2.4), if Ai was originally a function parameter pack: 在部分排序(14.8.2.4)期间,如果Ai最初是一个函数参数包:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM