C + + Decltype type descriptor _c language

Source: Internet
Author: User

1 Basic grammar

The Decltype type descriptor generates the type of the specified expression. In this procedure, the compiler analyzes an expression and obtains its type, but does not actually evaluate the value of an expression.

The syntax is:

Decltype (expression)

The compiler uses the following rules to determine the type of the expression parameter.

If the expression parameter is an identifier or class member access, then Decltype (expression) is the type of the entity named by expression. If no such entity or expression parameter names a set of overloaded functions, the compiler generates an error message.
If the expression parameter is a call to a function or an overloaded operator function, decltype (expression) is the return type of the function. The parentheses on either side of the overloaded operator is ignored.
If the expression parameter is a right value, then decltype (expression) is the expression type. If the expression parameter is a left value, then decltype (expression) is the expression of the reference type to the left value.
Give the following sample code:

int var;
Const int&& FX (); 
struct A {double x;}
Const A * a = new A ();

Statement type Comment
Decltype (FX ()); const int && const INT referenced to left value
Decltype (VAR); Type of the int variable var
Decltype (A->X); Type of double member access
Decltype ((a->x)); The const double& inner bracket Causes the statement to be evaluated as an expression rather than as a member. Because a is declared as a const pointer, the type is a reference to the const double.

2 Decltype and references

If Decltype uses an expression other than a variable, DECLTYPE returns the type of the expression result. But sometimes, some expressions return a reference type to Decltype. In general, when this happens, it means that the resulting object of the expression can be used as the left value of an assignment statement:

The result of the Decltype can be a reference type
int i =, *p = &i, &r = i;
Decltype (r + 0) b; OK, the result of the addition is int, so B is an (uninitialized) int
decltype (*p) C;//Error, C is int&

Because R is a reference, the result of Decltype (R) is a reference type, and if you want the result type to be the type that r refers to, you can take R as part of the expression, such as r+0, and obviously the result of this expression will be a specific value rather than a reference.

On the other hand, if the content of an expression is a dereference operation, then Decltype will get the reference type. As we are familiar with, the dereference pointer can get the object that the pointer refers to, and can also assign a value to the object, so the result type of Decltype (*p) is int& rather than int.

3 Decltype and Auto

Different ways to handle top const and references (reference reading: C + + Auto type descriptor)
If Decltype uses an expression that is a variable, DECLTYPE returns the type of the variable (including the top-level const and reference):

const int CI = 0, &cj = ci;
Decltype (CI) x = 0; The type of x is const int
decltype (CJ) y = x;//Y is a type of const int&,y bound to variable x
decltype (CJ) Z;//Error, Z is a reference and must be initialized

The result type of the decltype is closely related to the form of an expression

For the reference used by Decltype, if the variable name is prefixed with a pair of parentheses, the resulting type differs from the parentheses. If Decltype uses a variable with no parentheses, the result is the type of the variable, and if you add a layer or bracket to the variable, the compiler treats it as an expression.

Decltype ((i)) D; Error, D is int&, and must initialize
decltype (i) e;  OK, E is an uninitialized int

return type of template function

In c++11, you can use the Decltype type descriptor and the Auto keyword on a trailing return type to declare a template function whose return type relies on its template parameter type.
In c++14, you can use Decltype (auto) without a trailing return type to declare a template function whose return type depends on its template parameter type.
For example, define a sum template function:

C++11
 Template<typename T, TypeName u>
auto MyFunc (t&& T, u&& U)-> Decltype (forward <T> (t) + forward<u> (u)) 
    {return forward<t> (t) + forward<u> (u);};

c++14
Template<typename T, TypeName u>
decltype (Auto) myFunc (t&& T, u&& U) 
    { Return forward<t> (T) + forward<u> (U); };

(Forward: If the parameter is a right or right value reference, conditionally cast its arguments to the right reference.) )

Attach a section of source code:

#include <iostream> #include <string> #include <utility> #include <iomanip> using namespace std; Template<typename T1, typename t2> auto Plus (t1&& t1, t2&& T2)-> Decltype (forward<t1>

(t1) + forward<t2> (T2)) {return forward<t1> (T1) + forward<t2> (t2);}
  Class X {friend x operator+ (const x& x1, const x& x2) {return X (X1.m_data + x2.m_data);
public:x (int data): m_data (data) {} int Dump () const {return m_data;} private:int m_data;

};
  int main () {//Integer int i = 4;

  cout << "Plus (i, 9) =" << Plus (I, 9) << Endl;
  Floating point Float DX = 4.0;
  float dy = 9.5;

  cout << setprecision (3) << "Plus (dx, dy) =" << plus (dx, dy) << Endl;
  String string hello = "Hello,";
  String world = "world!";

  cout << Plus (Hello, world) << Endl;
  Custom type X x1 (20);
  X x2 (22); X x3 = Plus (x1, x2); cout << "X3. Dump () = "<< x3.
Dump () << Endl;

 }

The results of the operation are:

Plus (i, 9) =
Plus (dx, dy) = 13.5
Hello, world!
X3. Dump () = 42

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.