forked from YunWGui/cpp_study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauto.cpp
More file actions
154 lines (118 loc) · 2.33 KB
/
auto.cpp
File metadata and controls
154 lines (118 loc) · 2.33 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright (c) 2020 by Chrono
//
// g++ auto.cpp -std=c++11 -o a.out;./a.out
// g++ auto.cpp -std=c++14 -o a.out;./a.out
// g++ auto.cpp -std=c++14 -I../common -o a.out;./a.out
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <signal.h>
void case1()
{
int i = 0;
double x = 1.0;
std::string str = "hello";
std::map<int, std::string> m = {{1,"a"}, {2,"b"}};
std::map<int, std::string>::const_iterator iter = m.begin();
//using namespace std;
//cout << i << x;
}
void case2()
{
auto i = 0;
//auto i{0} ;
auto x = 1.0;
auto str = "hello";
//decltype("hello") str = "hello";
std::map<int, std::string> m = {{1,"a"}, {2,"b"}};
auto iter = m.begin();
auto f = bind1st(std::less<int>(), 2);
//using namespace std;
//cout << typeid(str).name() << endl;
}
void case3()
{
auto x = 0L;
auto y = &x;
auto z {&x};
}
class X final
{
//auto a = 10;
int a = 10;
};
void case4()
{
auto x = 10L;
auto& x1 = x;
auto* x2 = &x;
const auto& x3 = x;
auto x4 = &x3;
using namespace std;
cout << *x2 << endl;
cout << *x4 << endl;
//cout << typeid(x4).name() << endl;
}
void case5()
{
int x = 0;
decltype(x) x1;
decltype(x)& x2 = x;
decltype(x)* x3;
decltype(&x) x4;
decltype(&x)* x5;
decltype(x2) x6 = x2;
using int_ptr = decltype(&x);
using int_ref = decltype(x)&;
}
void case6()
{
int x = 0;
decltype(auto) x1 = (x);
decltype(auto) x2 = &x;
decltype(auto) x3 = x1;
}
auto get_a_set()
{
std::set<int> s = {1,2,3};
return s;
}
void case7()
{
using namespace std;
vector<int> v = {2,3,5,7,11};
for(const auto& i : v) {
cout << i << ",";
}
cout << endl;
for(auto& i : v) {
i++;
cout << i << ",";
}
cout << endl;
}
class DemoClass final
{
public:
using sig_func_ptr_t = decltype(&signal) ;
public:
using set_type = std::set<int>;
private:
set_type m_set;
using iter_type = decltype(m_set.begin());
iter_type m_pos;
};
int main()
{
using namespace std;
case1();
case2();
case3();
case4();
case5();
case6();
case7();
cout << "auto/decltype demo" << endl;
}