[C ++] references cited initialization, folding rules reference

Quote

  1. Reference is like a name for a memory area for storing data (variable), the definition of reference as though declares a variable name and bind it to the existing variables, variable name comes attributes (including but not limited to, the type of storage ), the variable name that comes from the name of the attribute used when declaring variables 声明指定符decision. Note that, for declaring lvalue references & and && for declaring the right values quoted are not 声明指定符, they are 声明符part of, which determine the value of the left / right value of the property referenced - left and right values of reference values references.

  2. By using the variable name (read-write) memory area (variable), the variable name that is included with the property is appended to the variable (a reference value Chuzuo / rvalue reference to the attributes), link codes:

    int a = 10;				// 定义一个变量,并声明一个变量名a,变量名a附带的属性是int
    const int &refa = a;	 // 声明变量a的一个名字(引用)refa,变量名refa附带的属性是const int
    refa = 15;				// 非法,通过refa使用变量,变量的属性是const int,因此不能进行赋值
    
  3. A variable, if not the variable name (including references) bound to the variable, then this variable is the right value, if whom bound variable name (including references), this variable becomes the left value. The value of the variable left / right value of property is not included with the name of the variable reference value left / right reference value property affects only tied to the memory area.

Cited initialization

  1. Reference must be initialized, initialization is cited references bound to a variable.
  2. The value of the variable left / right value of property is an inherent property of the memory area, regardless of the variable names.
  3. When referring to initialize variables bound property should be initialized and a reference to exactly match the properties, in particular, left a reference value can be bound to the value of the variable left and right reference value can only be bound to the value of the variable on the right. (Except for the first 4:00 exceptions, point 5 and point 6 mentioned)
  4. On const property, the property comes with const reference variable can be bound to a non-const incidental attribute.
  5. Value on the left / right in terms of property value, const lvalue reference can be bound to the right value of the variable.
  6. On class property, the parent class reference can be bound to subclasses. (Polymorphic)

When it comes to references ...

When defining a reference after reference to the cited references indicate that this variable is bound. (The only exception is with regard to decltypethe type of indicator) look at the code:

int &&a = 10;	/*定义了一个int类型的右值变量,变量的值是10,然后声明了一个
				 右值引用a并将a绑定到这个变量上,于是这个变量成为左值变量*/

int &b = a;		/*这里的a表示的是a所绑定的变量,这个变量的属性是int、左值变
				 量,因此可以将左值引用b绑定到这个变量上*/

The function parameter is a reference

If the function parameter is a reference, then the function parameter initialization function is invoked when it is bound to the variable function argument indicates, this process still follow the rules cited initialized. Let's look at a piece of code:

void testRight(int &&ref)
{}
void testLeft(int &ref)
{}

int &&ref15 = 15;		//合法
testRight(15);		    //合法,函数的实参表示一个值为15的右值变量
testRight(ref15);		//非法,试图将函数形参绑定到左值变量
testLeft(ref15);		//合法,函数实参表示一个值为15的左值变量

Function returns a reference

Similar to the reference variable as a function parameter when the function returns a reference, the initialization function return values ​​is indicated after a return statement to initialize the return value, this process is still comply with the rules cited initialized.

References folding rule

Folding rule for deriving a reference type.

Folding rule references:

Left value X & & <=> X &, the left reference value is equivalent to the cited references left value

X & && <=> X &, the left reference value is equivalent to the reference values ​​of the left and right reference value

X && & Lvalue <=> X &, rvalue references cited reference value is equivalent to the left

X && && <=> X &&, rvalue rvalue references cited reference is equivalent to the right value

In C ++, a direct statement cited references are illegal, but allows "indirect" the statement cited references is allowed. Nevertheless, the "indirect" the statement cited references or to be converted into a single reference. It is a reference conversion rule folding rule. References folding rule will be used in the following four contexts:

  • Examples of the folding rule which case reference template parameter is used to infer a valid type parameter. Look at the code:

    template <typename T> void test(T &&val)
    {}
    
    int i = 0; const int ci = i;
    test(i);					// T被推断为 int&,val的类型是 int& &&,折叠为 int&
    test(ci);					// T被推断为 const int&,val的类型是 const int& &&,折叠为 const int&
    test(i * ci);				// T被推断为 int,val的类型是 int&&
    
  • typedef or alias, in this case the folding rule references cited references are used to process the "indirect" announcement. Look at the code:

    typedef int &&INTR;			// int类型的右值引用类型
    INTR &&intrr = 5;		    // 右值引用的右值引用,折叠为右值引用
    
  • auto type specifier, this case is similar to the first case, the folding rule is used references infer a valid type. Look at the code:

    int a = 10;
    auto &&b = a;				// auto类型被推断为 int&,b的类型为 int& &&,折叠为 int&
    auto &c = 8;				// 错误,即使利用引用折叠规则也无法推断出合法的类型
    
  • decltype type indicator, in this case is similar to the second case, the folding rules are used to process references cited references "indirect" statement. Look at the code:

    int a = 5;
    decltype((a)) &&b = a;		// decltype推断出的类型是 int&,b的类型是 int& &&,折叠为 int&
    
Published 19 original articles · won praise 23 · views 3893

Guess you like

Origin blog.csdn.net/weixin_43737206/article/details/103516871