Decltype use the new features of C ++ 11 keyword

Keyword introduce a .decltype

Decltype similar keywords auto keyword, but there are differences between; auto keyword is used to determine the type represented at compile time by auto variables that have been initialized. In other words, auto modification of expression must be a variable has been initialized; then if we just want to get this type of variable, but so how? This time the turn decltype played, decltype keyword is used to deduce the type at compile time an expression, but this expression is initialized or not, the compiler will not have much impact.

Here is an example using the keyword decltype keyword:

#include <iostream>
using namespace std;

int main ()
{
    int x = 0;
    decltype(x) y = 1;  //y -> int
    cout << y << endl;
    decltype(x + y) z = 2; //z -> int
    cout << z << endl;

    const int& i = x;
    decltype(i) j = y;  //j -> const int &

    cout << j << endl;

    const the decltype (Z) * P = & Z;   // * P -> const int 
    the decltype (Z) * PI = & Z;        // * PI -> int, PI-> int * 
    the decltype (PI) * PP = & PI;      / / * PP -> int *, PP-> int ** 

    COUT << PP << endl;          // print result: 0x61fe80 
    COUT * PP << << endl;         // print result: 0x61fe84 
    COUT PP ** << < <endl;        // print the results: 2

    return 0;
}

Ay and z results show decltype can be directly deduced based on the type of variable expressions, and this function much like the auto, but different, auto only an expression derived variables should have according to the type of variable initialization, if you want to to get through some type of expression, but do not want new variables and this expression has the same value, then this is not as useful when the auto.

The results show that by Bj decltype type of expression obtained, can retain the expression and the const reference, can be accurately derived decltype expression defines the type itself, not as referenced discard in some cases as auto and cv qualifier.

Cp, pi results show decltype can be like, like auto, plus references and pointers, and cv qualifiers.

D.pp derivation described, when the expression is a pointer, the decltype still be able to deduce the actual type (pointer type) expression, after binding of the pointer mark pp definitions, pp obtained is a two-dimensional pointer type

Two .decltype inference rules

decltyp (exp) derivation rules are as follows:

(1) An identifier expression and access class expression.

(2) The function calls (non-identifier expression, nor the type of access expression).

(3). Parenthesized expressions and expressions adder (otherwise)

First is the first case:

#include <iostream>
using namespace std;

class Test
{
public:
    Test() {}

public:
    static const int nNumber = 0;
    int x;
};

int main ()
{

    // class access expression 
    the decltype (the Test :: nNumber) C = 10 ;
    Test test;
    decltype(test.x) d = 20;
    cout << c << "," << d <<endl;

    // Identifier expression 
    int Y = 20 is ;
    Decltype (s) with a = 30 ;
   return  0 ;
}

The second case: function call

#include <iostream>

using namespace std;

class Test
{
public:
    Test() {}

public :
     int m_nNum = - 10 ;
};

int Test_Int ();              // pure rvalue 
int & Test_Int_One ();         // left value

const  int Test_Cint ();       // pure rvalue 
const  int & Test_Cint_One (); // left value

const the Test test_class ();     // pure rvalue

int main ()
{
    int x = 10;

    decltype(Test_Int()) a1 = x;        //a1->int
    cout << a1 << endl;

    decltype(Test_Cint_One()) a2 = x;   //a2->int&
    int y = a2;
    cout << y << endl;
    cout << a2 << endl;

    decltype(Test_Cint()) b1 = x;       //b1->const int
    b1 = 20;
    cout << b1 << endl;

    the decltype (Test_Cint_One ()) B2 = X;    // B2-> const int &
     // B2 = 30;                             // error: B2 only a read-only reference, can no longer assign a value 
    COUT B2 << << endl;

    decltype(Test_Class()) c1 = Test();
    cout << c1.m_nNum << endl;

    return 0;
}

Return Value Type can be seen that according to the result of rule 2 and functions derived decltype consistent, it should be noted that instead of b1 is int const int, because the function returns an int pure rvalue, the right value for pure words, only class type can carry cv qualifiers, in addition to the usually ignored cv qualifier; therefore decltype deduced b1 is an int, and c1 derived type is const Test.

Guess you like

Origin www.cnblogs.com/QingYiShouJiuRen/p/11391089.html