C++11 Decltype

Source: Internet
Author: User

C++11 Decltype

Decltype is actually a bit like auto's inverse function, Auto lets you declare a variable, and decltype can get the type from a variable or an expression. Introduced in the C++11 standard, Decltype is designed primarily for generic programming to solve problems in generic programming that are difficult (and even impossible) to represent because some types are determined by template parameters. Decltype cannot be used in derived class declarations and destructor calls.
Similar to the sizeof operator, Decltype does not need to evaluate its operands. Roughly speaking, Decltype (e) has been deduced as follows before returning a type:?

If the expression e points to a local variable, namespace scope variable, static member variable, or function parameter, then the return type is the "claim type" of the variable (or parameter);
If E is an lvalue (Lvalue, or "addressable value"), then Decltype (e) will return T&, where T is the type of E;
If E is an X value (Xvalue), the return value is t&&;?
If E is a pure right value (prvalue), the return value is T.

#define_crt_secure_no_warnings#include<iostream>#include<string>#include<vector>#include<map>voidmytest () {inti; Decltype (i) J=0; Std::cout<< typeID (j). Name () << Std::endl;//J---> int    floatA; Doubleb; Decltype (A+b) C; Std::cout<< typeID (c). Name () << Std::endl;//c---> Doublestd::vector<int>VEC;    typedef decltype (Vec.begin ()) Vectype;    Vectype K; Std::cout<< typeID (k). Name () <<Std::endl;  for(k = Vec.begin (); k < Vec.end (); k++)    {        //Do some thing ...    }    enum //Anonymous Enumeration variables{OK, Error, Warning} flag; DECLTYPE (flag) TMP=OK; Std::cout<< typeid (TMP). Name () <<Std::endl; return;}intMain () {mytest (); System ("Pause"); return 0;}

C++11 Decltype

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.