名前空間
変種
操作

reference initialization

提供: cppreference.com
< cpp‎ | language

 
 
C + +言語
一般的なトピック
Original:
General topics
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
フロー制御
Original:
Flow control
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
条件付き実行文
Original:
Conditional execution statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
繰り返し文
Original:
Iteration statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
文をジャンプします
Original:
Jump statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
機能します
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
関数の宣言
ラムダ関数の宣言
関数テンプレート
の歴史。インライン指定
例外仕様 (廃止予定)
noexcept指定子 (C++11)
例外
Original:
Exceptions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
名前空間
Original:
Namespaces
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
タイプ
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
decltype specifier (C++11)
指定子
Original:
Specifiers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
CV指定
貯蔵期間指定
constexprの指定子 (C++11)
自動指定 (C++11)
alignas指定子 (C++11)
初期化
Original:
Initialization
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
集約の初期化
リストの初期化 (C++11)
定数の初期化
リファレンスの初期化
リテラル
Original:
Literals
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Original:
Expressions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
代替表現
ユーティリティ
Original:
Utilities
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
タイプ
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
typedef declaration
型の別名宣言 (C++11)
属性 (C++11)
キャストします
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
暗黙の型変換
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
Cスタイルキャストと機能
メモリの割り当て
Original:
Memory allocation
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
クラス
Original:
Classes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
クラス固有の機能特性
Original:
Class-specific function properties
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
明示 (C++11)
静的
特殊なメンバ関数
Original:
Special member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
テンプレート
Original:
Templates
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
クラステンプレート
関数テンプレート
テンプレートの特殊化
パラメーターパック (C++11)
その他
Original:
Miscellaneous
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
インラインアセンブリ
 
オブジェクトへの参照をバインドします
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.

目次

[編集] 構文

T & ref = object ;

T & ref ( object ) ;

T & ref { object } ;

(1)
T && ref = object ;

T && ref ( object ) ;

T && ref { object } ;

(2) (C++11およびそれ以降)
R fn ( T & arg );

or

R fn ( T && arg );

fn ( object )

(3)
T & fn () {

or

T && fn () {

return object ;

(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.
参照は、次のような状況では初期化されます
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.
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.
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.
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.
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.
リファレンスの初期化の効果は、次のとおりです
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.
  • 参照は左辺値参照されている場合、
    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 is T or a base of T, 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 either T or a base of T, 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 either T or a base of T, 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 either T or a base of T, 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 type T 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.
  • 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.

[編集] ノート

参照はクラスメンバの宣言で、関数の戻り値の型宣言では、唯一の関数パラメータ宣言で初期化せずに表示され、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.

[編集]

#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;
}


[編集] も参照してください