C ++中的类型推断(自动和decltype)
在本教程中,我们将讨论一个程序来理解C++中的类型干扰(自动和decltype)。
如果使用auto关键字,则变量的类型是根据其初始值设定项的类型定义的。进一步使用decltype,它使您可以从被调用的元素中提取变量的类型。
自动类型
示例
#include <bits/stdc++.h>
using namespace std;
int main(){
auto x = 4;
auto y = 3.37;
auto ptr = &x;
cout << typeid(x).name() << endl
<< typeid(y).name() << endl
<< typeid(ptr).name() << endl;
return 0;
}输出结果
i d Pi
定义类型
示例
#include <bits/stdc++.h>
using namespace std;
int fun1() { return 10; }
char fun2() { return 'g'; }
int main(){
decltype(fun1()) x;
decltype(fun2()) y;
cout << typeid(x).name() << endl;
cout << typeid(y).name() << endl;
return 0;
}输出结果
i c
热门推荐
10 小红书平安祝福语简短
11 生日祝福语大全女孩简短
12 收生日红包祝福语 简短
13 领证幽默祝福语简短
14 法考面试祝福语简短
15 老哥出门祝福语简短语
16 送灯祝福语简短独特
17 幼儿狗年祝福语大全简短
18 好听的元旦简短祝福语