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