C++11新特性概览

今天买的《C++ Primer 第五版》到了,这一版本一个比较好的地方是。在开始的目录里面列出来了全书中涉及到的C++11新特性的地方,标明了页码,可以直接到对应的页面去看新特性的东西。于是我对照书上的例子,写了一些简单的示例,用来大概的了解C++11的新特性,总结在这里,以后可以查查。

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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include <iostream>
#include <cstdlib>
#include <vector>
#include <array>
#include <iterator>
#include <functional>

using namespace std;

constexpr int size(){return 100;} // constexpr function, compiler will convert it to inline function

void func(initializer_list<int> li) {
cout << "func with " << li.size() << "elements" << endl;
}

//
vector<int> func2() {
return {1,2,3};
}


int main() {
long long ll = 64; //long long: 64 bit at least

//2. list initialization
int a = {0};
int bb{0};
vector<int> v2 = {1,2,3};


//3. nullptr, a new literal
int *p = nullptr;
//equals to
int *p2 = 0;
int *p3 = NULL;// must include <cstdlib> first

//4. constexpr
constexpr int ci = 30;
constexpr float cf = ci * 0.2;
constexpr int sz = size(); // it's ok to initialize a constexpr value using constexpr function

//5. alias declaration
using my_int = int;
my_int i = 10;

using vi = vector<int>;
vi v = {1,2,3,5};

using it = vector<int>::iterator;
for (it it_i = v.begin(); it_i != v.end(); ++it_i) {
cout << *it_i << endl;
}


//6. auto
auto aa = 3; // compiler will infer that a's type is int

//7.decltype
decltype(i) id1 = 0; // id1 has the same type with i
decltype((i)) id2 = id1; // NOTE: (var) returns reference! so id2 is a reference to id1

//7. in-class initializer
/*
struct my_struct {
int i = 0;
int b = 2;
string s;
};
*/

//8. range for
string hello = "hello, world!";
for (auto c : hello) {
cout << c << endl;
}

//9. vector of vector, no space anymore!
//before:
vector<vector<int> > vv1;
//now:
vector<vector<int>> vv2;

//10. cbegin, cend, return const_iterator don't care if container is const
vector<int> vi10 = {4,4,54};
const vector<int> cvi10 = {4,4,54};
auto it10 = vi10.cbegin(); // it10: const_iterator type
auto cit10 = cvi10.cbegin(); // cit10: const_iterator type

//11. begin and end of pointer, included in <iterator>
int ia[] = {1,3,4};
int *b = begin(ia);
int *e = end(ia);

//12. initializer_list
func({1,2,3,4});

//13. trailing return type
/*
auto func(int i) -> int(*)[10];
*/

//14. =default: don't replace default initializer function
/*
class A {
public:
A() = default;
};
*/

//15. delegating constructor
/*
class A {
public:
A(int i, float f, double d):ii(i), ff(f), dd(d) {}
A(int i, float f): A(i, f, 0) {}
A(int i): A(i, 0, 0) {}

private:
int ii;
float ff;
double dd;
};
*/

//16. array
array<int, 3> a1 = {{1,2,3}};
auto aa1 = a1.begin();

//17.forward_list

//18. emplace

//19. shrink_to_fit

//20. to_string
int i20 = 233;
string s20 = to_string(i20);

//21. lambda
auto f211 = [] () {return 23;};
auto f212 = [i20]() {return i20 > 20;};
auto f213 = [i20]() -> int {if(i20> 20) return 1; else return 0;};

//22. bind, included in <functional>
/*
int f(int a, int b) {
return a>b ? a : b;
}

auto g = bind(f, _1, 10);
*/

//23. unordered associative container, managed by hash
//including unordered_map, unordered_set, unordered_multimap, unordered_multiset

//24. smart pointer,including shared_ptr, unique_ptr, weak_ptr

//25. rvalue reference
int i25 = 42;
int && rr = i25*42;

//ERROR: variable is lvalue
//int &&rr2 = rr;

//26. std::move
int &&rr3 = std::move(rr);

//27. noexcept,use for moving copy or moving operator

//28. reference qualifier
/*
class A{
public:
A sorted() &&; //rvalue reference
A sorted() &; //lvalue reference
};
*/

//29. function template, included in <functional>
//function<int(int, int)> f1 = add; //add is a function declared before

//30. explicit conversion operator
/*
class A {
explicit operator int() const {return val;};
};
*/

//31. final to prevent inherit
class Last final {}; // Last can't be a basic class

//32.override: override virtual functions in parents class

//33. tuple
tuple<int, int, int> tt{1,2,4};
auto tt1 = get<0>(tt);
auto tt2 = get<1>(tt);
auto tt3 = get<2>(tt);


return 0;
}