C++11 decltype關鍵字

C++11 decltype關鍵字

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
*/

decltype和auto的差別:

1.auto忽略頂層const,decltype保留頂層constspa

2.對引用操做,auto推斷出原有類型,decltype推斷出引用code

3.對解引用操做,auto推斷出原有類型,decltype推斷出引用編譯器

4.auto推斷時會實際執行,decltype不會執行,只作分析。io

相關文章
相關標籤/搜索