[C + +] Decltype Type indicator

Source: Internet
Author: User

In some cases, we want to infer the type of the variable to be defined from the type of the expression, but do not want to initialize the variable with the value of the expression. The second type descriptor Delctype introduced by C++11 is used to select and return the data type of the operand. The compiler parses an expression and gets its type, but does not actually evaluate the value of the expression:

    decltype (size(5)) s = i;‘    decltype返回size函数返回值的类型,来声明变量s。

Decltype handling the top-level const and reference methods differs from auto, and if the expression used by Decltype is a variable, DECLTYPE returns the type of the variable, including the top-level const and the reference.

    constint0, &cj = ci;    decltype0;             // x的类型是const int    decltype(cj) y = x;             // y的类型是const int&, y绑定到x    decltype(cj) z;                 // error, z是一个引用,必须初始化

If the expression used by Decltype is not a variable, DECLTYPE returns the type corresponding to the expression result. Some expressions return a reference type to Decltype, which means that the resulting object of the expression can be an lvalue of an assignment statement.

    int42*p = &i, &r = i;    0) b;                  // 加法的结果是int, b的类型就是int    decltype(*p) c;                     // error, c为int &

Because R is a reference, the result of Decltype (R) is a reference type, and for an int type, R is part of the expression, such as r+0.
If the content of an expression is a dereference operation, Decltype will get the reference type.
For the expression used by Decltype, if the variable is given a pair of parentheses, the resulting type will be different from the parentheses. The parentheses compiler treats it as an expression, and without parentheses returns the type of the variable directly.

    decltype((i)) d;                    // error, decltype的表达式如果是加上了括号的变量,结果为引用    decltype(i) e;                      // e是一个未初始化的int

The assignment produces a reference, the type of the reference is the type of the lvalue, and if I is Int,i = the type of x is Int&

    a34;    decltype(aa;    decltype(aa;

[C + +] Decltype Type indicator

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.