decltype做用是返回操做數的數據類型,編譯器分析表達式並獲得它的類型,卻不實際計算表達式的值。ios
#include <iostream> using namespace std; int test(int temp) { return 10; } int main() { int a = 2; decltype(a) b; b = 3; cout << a << " " << b << endl; decltype(test(a)) c; c = 4; cout << c << endl; return 0; } /* 輸出 2 3 4 */
1.auto忽略頂層const,decltype保留頂層constspa
2.對引用操做,auto推斷出原有類型,decltype推斷出引用code
3.對解引用操做,auto推斷出原有類型,decltype推斷出引用編譯器
4.auto推斷時會實際執行,decltype不會執行,只作分析。io