forked from Light-City/CPlusPlusThings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decltype.cpp
47 lines (37 loc) · 890 Bytes
/
decltype.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <vector>
using namespace std;
/**
* 泛型编程中结合auto,用于追踪函数的返回值类型
*/
template <typename T>
auto multiply(T x, T y) -> decltype(x * y) {
return x * y;
}
int main() {
int nums[] = {1, 2, 3, 4};
vector<int> vec(nums, nums + 4);
vector<int>::iterator it;
for (it = vec.begin(); it != vec.end(); it++)
cout << *it << " ";
cout << endl;
using nullptr_t = decltype(nullptr);
nullptr_t nu;
int *p = NULL;
if (p == nu)
cout << "NULL" << endl;
typedef decltype(vec.begin()) vectype;
for (vectype i = vec.begin(); i != vec.end(); i++)
cout << *i << " ";
cout << endl;
/**
* 匿名结构体
*/
struct {
int d;
double b;
} anon_s;
decltype(anon_s) as{1, 2.0}; // 定义了一个上面匿名的结构体
cout << multiply(11, 2) << ":" << as.b << endl;
return 0;
}