reference initialization
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
绑定到一个对象的引用
目录 |
[编辑] 语法
T & ref = object ;
T T |
(1) | ||||||||
T && ref = object ;
T T |
(2) | (C++11 起) | |||||||
R fn ( T & arg );
or R fn fn |
(3) | ||||||||
T & fn () {
or T
|
(4) | ||||||||
[编辑] 解释
的参考
T
可以初始化对象的类型T
,T
类型的函数,或隐式转换为T
的对象。初始化完成后,参考不能更改指向另一个对象.....原文:
A reference to
T
can be initialized with an object of type T
, a function of type T
, or an object implicitly convertible to T
. Once initialized, a reference cannot be changed to refer to another object.参考在下列情况下被初始化
原文:
References are initialized in the following situations:
1)
当一个命名的左值引用变量的声明带有初始值
原文:
When a named lvalue reference variable is declared with an initializer
2)
当一个命名的右值引用变量的初始值设定项声明
原文:
When a named rvalue reference variable is declared with an initializer
3)
在一个函数调用表达式,函数的参数时,引用类型
原文:
In a function call expression, when the function parameter has reference type
4)
在return表,当函数返回一个引用类型
原文:
In the return statement, when the function returns a reference type
参考电压的初始化的效果是:
原文:
The effects of reference initialization are:
- 如果参考是一个左值引用原文:If the reference is an lvalue reference:
- object是一个左值表达式,它的类型是
T
T
的基地,是同样或更小的品种合格的,然后引用绑定的左值或基类的子对象的对象标识的对象.原文:If object is an lvalue expression, and its type isT
or a base ofT
, and is equally or less cv-qualified, then the reference is bound to the object identified by the lvalue or the base class subobject of the object.
-
- 如果object是一个左值表达式,它的类型是隐式转换的类型,要么是
T
或T
基地,同样或更少的合格的简历,然后返回非显式转换函数的源类型和它的基类,左值的引用被认为是最好的选择重载决议。然后绑定到返回的左值转换函数(或它的基类子对象)标识的对象的引用原文:If object is an lvalue expression, and its type is implicitly convertible to a type that is eitherT
or a base ofT
, equally or less cv-qualified, then the non-explicit conversion functions of the source type and its base classes that return lvalue references are considered and the best one is selected by overload resolution. The reference is then bound to the object identified by the lvalue returned by the conversion function (or its base class subobject)
-
- 否则,如果参考要么是rvalue引用或左值参照const:原文:Otherwise, if the reference is either rvalue reference or lvalue reference to const:
- object值为xValue,一类prvalue,数组prvalue,或的功能左值类型,要么是
T
或基地,T
,同样或更小的简历合格的,然后参考的初始化表达式的值,势必或其基子.原文:If object is an xvalue, a class prvalue, an array prvalue, or a function lvalue type that is eitherT
or a base ofT
, equally or less cv-qualified, then the reference is bound to the value of the initializer expression or its base subobject.
-
- 如果object是一个类类型的表达式可以隐式转换的xvalue,的类prvalue,或一个函数值的类型,要么是
T
或基地,T
,同样或较少的品种合格,则引用绑定到转换的结果或它的基类子对象.原文:If object is a class type expression that can be implicitly converted to an xvalue, a class prvalue, or a function value of type that is eitherT
or a base ofT
, equally or less cv-qualified, then the reference is bound to the result of the conversion or its base subobject.
-
[编辑] 一个临时的寿命
当提到绑定到一个临时的或基类子对象的一个临时的,临时的寿命延长的寿命相匹配的参考,但下列情况除外:
原文:
Whenever a reference is bound to a temporary or to a base subobject of a temporary, the lifetime of the temporary is extended to match the lifetime of the reference, with the following exceptions:
- 在return语句的函数的返回值的绑定到一个临时不延伸:它被立即销毁结束时返回表达式。这样的函数总是返回一个悬空的引用.原文:a temporary bound to a return value of a function in a return statement is not extended: it is destroyed immediately at the end of the return expression. Such function always returns a dangling reference.
- 一个临时的引用参数在函数调用一直存在,直到充分体现该函数调用结束时,如果该函数返回一个引用,它会超越充分的表达,它成为一个悬空的引用.原文:a temporary bound to a reference parameter in a function call exists until the end of the full expression containing that function call: if the function returns a reference, which outlives the full expression, it becomes a dangling reference.
- 一个临时的存在势必在初始化一个新的表达式中使用的一个参考的充分体现,新的表达,而不是只要初始化的对象,直到结束。如果初始化的对象会超越充分的表达,它的引用成员成为悬空的引用.原文:a temporary bound to a reference in the initializer used in a new-expression exists until the end of the full expression containing that new-expression, not as long as the initialized object. If the initialized object outlives the full expression, its reference member becomes a dangling reference.
在一般情况下,一个临时的寿命可以进一步延长“传递给它的”第二个参考,初始化的参考,暂时被捆绑,不影响其寿命.
原文:
In general, the lifetime of a temporary cannot be further extended by "passing it on": a second reference, initialized from the reference to which the temporary was bound, does not affect its lifetime.
[编辑] 注释
参考文献出现在声明函数的返回类型,没有初始化函数不仅在功能参数声明,在声明一个类的成员,并与
extern
说明.原文:
References appear without initializers only in function parameter declaration, in function return type declaration, in the declaration of a class member, and with the
extern
specifier.[编辑] 示例
#include <utility> #include <sstream> struct S { int mi; const std::pair<int,int>& mp; // reference member }; void foo(int) {} struct A {}; struct B : A { int n; operator int&() { return n; }; }; B bar() {return B(); } //int& bad_r; // error: no initializer extern int& ext_r; // OK int main() { // lvalues int n = 1; int& r1 = n; // lvalue reference to the object n const int& cr(n); // reference can be more cv-qualified volatile int& cv{n}; // any initializer syntax can be used int& r2 = r1; // another lvalue reference to the object n // int& bad = cr; // error: less cv-qualified int& r3 = const_cast<int&>(cr); // const_cast is needed void (&rf)(int) = foo; // lvalue reference to function int ar[3]; int (&ra)[3] = ar; // lvalue reference to array B b; A& base_ref = b; // reference to base subobject int& converted_ref = b; // reference to the result of a conversion // rvalues // int& bad = 1; // error: cannot bind lvalue ref to rvalue const int& cref = 1; // bound to rvalue int&& rref = 1; // bound to rvalue const A& cref2 = bar(); // reference to A subobject of B temporary A&& rref2 = bar(); // same int&& xref = static_cast<int&&>(n); // bind directly to n // int&& copy_ref = n; // error: can't bind to an lvalue double&& copy_ref = n; // bind to an rvalue temporary with value 1.0 // restrictions on temporary lifetimes std::ostream& buf_ref = std::ostringstream() << 'a'; // the ostringstream temporary // was bound to the left operand of operator<<, but its lifetime // ended at the semicolon: buf_ref is now a dangling reference. S a { 1, {2,3} }; // temporary pair {2,3} bound to the reference member // a.mp and its lifetime is extended to match a S* p = new S{ 1, {2,3} }; // temporary pair {2,3} bound to the reference // member a.mp, but its lifetime ended at the semicolon // p->mp is a dangling reference delete p; }