@@ -6,9 +6,168 @@ order: 9
6
6
7
7
# Chapter 09 Minor Features
8
8
9
- [ Table of Content] ( ./toc.md ) | [ Previous Chapter] ( ./08-filesystem.md ) | [ Next Chapter: Outlook: Introduction of C++20] ( ./10-cpp20.md )
9
+ [ TOC]
10
+
11
+ ## 9.1 New Type
12
+
13
+ ### ` long long int `
14
+
15
+ ` long long int ` is not the first to be introduced in C++11.
16
+ In fact, as early as C99, ` long long int ` has been included in the C standard,
17
+ so most compilers already support it.
18
+ C++11 now formally incorporate it into the standard library,
19
+ specifying a ` long long int ` type with at least 64 bits.
20
+
21
+ ## 9.2 ` noexcept ` Operations
22
+
23
+ One of the big advantages of C++ over C is that
24
+ C++ itself defines a complete set of exception handling mechanisms.
25
+ However, before C++11, almost no one used to write
26
+ an exception declaration expression after the function name.
27
+ Starting from C++11, this mechanism was deprecated,
28
+ so we will not discuss or introduce the previous mechanism.
29
+ How to work and how to use it, you should not take the initiative
30
+ to understand it.
31
+
32
+ C++11 simplifies exception declarations into two cases:
33
+
34
+ 1 . The function may throw any exceptions
35
+ 2 . The function can't throw any exceptions
36
+
37
+ And use ` noexcept ` to limit these two behaviors, for example:
38
+
39
+ ``` cpp
40
+ void may_throw (); // May throw any exception
41
+ void no_throw () noexcept ; // Cannot throw any exception
42
+ ```
43
+
44
+ If a function modified with ` noexcept ` is thrown,
45
+ the compiler will use ` std::terminate() ` to
46
+ immediately terminate the program.
47
+
48
+ ` noexcept ` can also be used as an operator to manipulate an expression.
49
+ When the expression has no exception, it returns ` true ` ,
50
+ otherwise it returns ` false ` .
51
+
52
+ ``` cpp
53
+ #include < iostream>
54
+ void may_throw () {
55
+ throw true;
56
+ }
57
+ auto non_block_throw = []{
58
+ may_throw ();
59
+ };
60
+ void no_throw () noexcept {
61
+ return;
62
+ }
63
+
64
+ auto block_throw = []() noexcept {
65
+ no_throw ();
66
+ };
67
+ int main ()
68
+ {
69
+ std::cout << std::boolalpha
70
+ << "may_throw() noexcept? " << noexcept(may_throw()) << std::endl
71
+ << "no_throw() noexcept? " << noexcept(no_throw()) << std::endl
72
+ << "lmay_throw() noexcept? " << noexcept(non_block_throw()) << std::endl
73
+ << "lno_throw() noexcept? " << noexcept(block_throw()) << std::endl;
74
+ return 0;
75
+ }
76
+ ```
77
+
78
+ ` noexcept ` can modify the function of blocking exceptions
79
+ after modifying a function. If an exception is generated internally,
80
+ the external will not trigger. For instance:
81
+
82
+ ``` cpp
83
+ try {
84
+ may_throw ();
85
+ } catch (...) {
86
+ std::cout << "exception captured from my_throw()" << std::endl;
87
+ }
88
+ try {
89
+ non_block_throw ();
90
+ } catch (...) {
91
+ std::cout << "exception captured from non_block_throw()" << std::endl;
92
+ }
93
+ try {
94
+ block_throw ();
95
+ } catch (...) {
96
+ std::cout << "exception captured from block_throw()" << std::endl;
97
+ }
98
+ ```
99
+
100
+ The final output is:
10
101
11
- ## Further Readings
102
+ ```
103
+ exception captured, from my_throw()
104
+ exception captured, from non_block_throw()
105
+ ```
106
+
107
+ ## 9.3 Literal
108
+
109
+ ### String Literal
110
+
111
+ In traditional C++, it is very painful to write a string full of
112
+ special characters. For example, a string containing HTML ontology
113
+ needs to add a large number of escape characters.
114
+ For example, a file path on Windows often as: ` C:\\Path\\To\\File ` .
115
+
116
+ C++11 provides the original string literals,
117
+ which can be decorated with ` R ` in front of a string,
118
+ and the original string is wrapped in parentheses, for example:
119
+
120
+ ``` cpp
121
+ #include < iostream>
122
+ #include < string>
123
+
124
+ int main () {
125
+ std::string str = R"(C:\Path\To\File)";
126
+ std::cout << str << std::endl;
127
+ return 0;
128
+ }
129
+ ```
130
+
131
+ ### Custom Literal
132
+
133
+ C++11 introduces the ability to customize literals by
134
+ overloading the double quotes suffix operator:
135
+
136
+ ``` cpp
137
+ // String literal customization must be set to the following parameter list
138
+ std::string operator " " _wow1(const char *wow1, size_t len) {
139
+ return std::string(wow1)+"woooooooooow, amazing";
140
+ }
141
+
142
+ std::string operator " " _wow2 (unsigned long long i) {
143
+ return std::to_string(i)+"woooooooooow, amazing";
144
+ }
145
+
146
+ int main () {
147
+ auto str = "abc"_wow1;
148
+ auto num = 1_wow2;
149
+ std::cout << str << std::endl;
150
+ std::cout << num << std::endl;
151
+ return 0;
152
+ }
153
+ ```
154
+
155
+ Custom literals support four literals:
156
+
157
+ 1 . Integer literal: When overloading, you must use ` unsigned long long ` , ` const char * ` , and template literal operator parameters. The former is used in the above code;
158
+ 2 . Floating-point literals: You must use ` long double ` , ` const char * ` , and template literals when overloading;
159
+ 3 . String literals: A parameter table of the form ` (const char *, size_t) ` must be used;
160
+ 4 . Character literals: Parameters can only be ` char ` , ` wchar_t ` , ` char16_t ` , ` char32_t ` .
161
+
162
+ ## Conclusion
163
+
164
+ Several of the features introduced in this section are those that
165
+ use more frequent features from modern C++ features that
166
+ have not yet been introduced. ` noexcept ` is the most important feature.
167
+ One of its features is to prevent the spread of anomalies,
168
+ effective Let the compiler optimize our code to the maximum extent possible.
169
+
170
+ [ Table of Content] ( ./toc.md ) | [ Previous Chapter] ( ./08-filesystem.md ) | [ Next Chapter: Outlook: Introduction of C++20] ( ./10-cpp20.md )
12
171
13
172
## Licenses
14
173
0 commit comments