reference initialization
提供: cppreference.com
![]() | このページは、Google 翻訳を使って英語版から機械翻訳されました。 翻訳には誤りや奇妙な言い回しがあるかもしれません。文章の上にポインタをおくと、元の文章が見れます。誤りを修正して翻訳を改善する手助けをしてください。翻訳についての説明は、ここをクリックしてください。 |
オブジェクトへの参照をバインドします
Original:
Binds a reference to an object
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
目次 |
[編集]構文
T& ref= object ; T T | (1) | ||||||||
T&& ref= object ; T T | (2) | (C++11およびそれ以降) | |||||||
Rfn( T& arg); or Rfn fn | (3) | ||||||||
T& fn() { or T
| (4) | ||||||||
[編集]説明
T
への参照は、タイプT
、型T
の関数、またはT
に暗黙的に変換可能なオブジェクトのオブジェクトで初期化することができます。いったん初期化されると、参照が別のオブジェクトを参照するように変更することはできません.Original:
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.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
参照は、次のような状況では初期化されます
Original:
References are initialized in the following situations:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
1)
名前の左辺値参照変数は初期化子を使用して宣言されたとき
Original:
When a named lvalue reference variable is declared with an initializer
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
2)
名前の右辺値参照変数は、初期化子を使用して宣言されたとき
Original:
When a named rvalue reference variable is declared with an initializer
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
3)
関数のパラメータが参照型を持つ関数呼び出しで表現
Original:
In a function call expression, when the function parameter has reference type
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
4)
return文では、ときに関数が参照型を返します
Original:
In the return statement, when the function returns a reference type
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
リファレンスの初期化の効果は、次のとおりです
Original:
The effects of reference initialization are:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
- 参照は左辺値参照されている場合、Original:If the reference is an lvalue reference:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- objectは左辺値式であり、その型は
T
またはT
の底で、均等に以下のcv-修飾され、その参照は左辺値またはオブジェクトの基底クラスのサブオブジェクトで識別されるオブジェクトにバインドされている場合.Original: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.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- objectは左辺値式で、その型はその後、均等またはCV修飾以下、返すソース·タイプおよびその基底クラスの非明示的な変換関数
T
またはT
のベースのいずれかである型に暗黙的に変換する場合左辺値参照が考慮され、最高の1は、オーバーロードの解決で選択されています。参照は、変換関数(またはその基本クラスのサブオブジェクト)によって返された左辺値によって識別されたオブジェクトにバインドされていますOriginal: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)The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- それ以外の場合は、参照は右辺値参照またはconstに左辺値参照のいずれかである場合:Original:Otherwise, if the reference is either rvalue reference or lvalue reference to const:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- objectにはxValue、クラスprvalue、配列prvalue、または
T
またはT
のベースのどちらかである関数左辺値の型、均等に以下のcv-修飾されている場合、その参照は初期化式の値にバインドされているか、そのベースサブオブジェクト.Original: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.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- objectが暗黙のxValue、クラスprvalue、または
T
またはT
のベースのどちらかであるタイプの関数値に変換することができ、クラス型の式である場合、同じように以下のcv-修飾、その参照はにバインドされている変換またはその基本subobjectの結果.Original: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.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- それ以外の場合は、タイプ
T
の一時はコピー初期化から構築objectされています。参照は、この一時的にバインドされています.Original:Otherwise, a temporary of typeT
is constructed and コピー初期化 from object. The reference is then bound to this temporary.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
[編集]一時の寿命
参照は一時的にまたは一時のベースサブオブジェクトにバインドされているときはいつでも、一時の寿命は、次の例外を除いて、参照の寿命と一致するように拡張されます
Original:
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:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
- returnステートメント内の関数の戻り値にバインドされて一時は拡張されません:それは、リターン式の末尾に直ちに破棄されます。このような関数は常に宙ぶらりんの参照を返します.Original: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.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - コンストラクタ初期化子リストにおける基準部材にバインドされた一時的なオブジェクトが存在する限りではない、唯一のコンストラクタが終了するまで持続します.Original:a temporary bound to a reference member in a constructor 初期化子リスト persists only until the constructor exits, not as long as the object exists.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - 関数呼び出しに参照パラメータにバインドされて一時的には、その関数呼び出しを含む完全な式の終わりまで存在:関数は完全な表現を長生きの参照を返す場合、それはぶら下がり参照になります.Original: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.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - new式で使用される初期化子内の参照にバインドされて一時的には、初期化されたオブジェクトである限りではない、新しい式を含む完全な式の終わりまで存在します。初期化されたオブジェクトは、完全な表現を長生きした場合、その基準部材は、ぶら下がり参照になります.Original: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.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
一般的には、一時の寿命はさらに "それを渡すこと"が拡張することはできません:一時が結合した基準から初期第二の基準は、その有効期間には影響しません.
Original:
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.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[編集]ノート
参照はクラスメンバの宣言で、関数の戻り値の型宣言では、唯一の関数パラメータ宣言で初期化せずに表示され、
extern
指定子で.Original:
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.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[編集]例
このコードを実行します
#include <utility>#include <sstream>struct S {int mi;conststd::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 initializerexternint& ext_r;// OK int main(){// lvaluesint n =1;int& r1 = n;// lvalue reference to the object nconstint& cr(n);// reference can be more cv-qualifiedvolatileint& cv{n};// any initializer syntax can be usedint& r2 = r1;// another lvalue reference to the object n// int& bad = cr; // error: less cv-qualifiedint& r3 =const_cast<int&>(cr);// const_cast is needed void(&rf)(int)= foo;// lvalue reference to functionint ar[3];int(&ra)[3]= ar;// lvalue reference to array B b; A& base_ref = b;// reference to base subobjectint& converted_ref = b;// reference to the result of a conversion // rvalues// int& bad = 1; // error: cannot bind lvalue ref to rvalueconstint& cref =1;// bound to rvalueint&& 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 lvaluedouble&& copy_ref = n;// bind to an rvalue temporary with value 1.0 // restrictions on temporary lifetimesstd::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;}