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

相关文章
相关标签/搜索