Skip to content

Commit 9779283

Browse files
committed
see changkun#12: translate ch09
1 parent 3fc59f9 commit 9779283

File tree

6 files changed

+189
-24
lines changed

6 files changed

+189
-24
lines changed

book/en-us/09-others.md

Lines changed: 161 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,168 @@ order: 9
66

77
# Chapter 09 Minor Features
88

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:
10101

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)
12171

13172
## Licenses
14173

book/zh-cn/09-others.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,23 @@ order: 9
66

77
# 第 9 章 其他杂项
88

9-
> 内容修订中
10-
119
[TOC]
1210

1311
## 9.1 新类型
1412

1513
### `long long int`
1614

17-
`long long int` 并不是 C++11 最先引入的,其实早在 C99,`long long int` 就已经被纳入 C 标准中,所以大部分的编译器早已支持。C++11 的工作则是正式把它纳入标准库,规定了一个 `long long int` 类型至少具备 64 位的比特数。
15+
`long long int` 并不是 C++11 最先引入的,其实早在 C99,
16+
`long long int` 就已经被纳入 C 标准中,所以大部分的编译器早已支持。
17+
C++11 的工作则是正式把它纳入标准库,
18+
规定了一个 `long long int` 类型至少具备 64 位的比特数。
1819

1920
## 9.2 noexcept 的修饰和操作
2021

21-
C++ 相比于 C 的一大优势就在于 C++ 本身就定义了一套完整的异常处理机制。然而在 C++11 之前,几乎没有人去使用在函数名后书写异常声明表达式,从 C++11 开始,这套机制被弃用,所以我们不去讨论也不去介绍以前这套机制是如何工作如何使用,你更不应该主动去了解它。
22+
C++ 相比于 C 的一大优势就在于 C++ 本身就定义了一套完整的异常处理机制。
23+
然而在 C++11 之前,几乎没有人去使用在函数名后书写异常声明表达式,
24+
从 C++11 开始,这套机制被弃用,所以我们不去讨论也不去介绍以前这套机制是如何工作如何使用,
25+
你更不应该主动去了解它。
2226

2327
C++11 将异常的声明简化为以下两种情况:
2428

@@ -93,16 +97,19 @@ try {
9397

9498
### 原始字符串字面量
9599

96-
传统 C++ 里面要编写一个充满特殊字符的字符串其实是非常痛苦的一件事情,比如一个包含 HTML 本体的字符串需要添加大量的转义符,例如一个Windows 上的文件路径经常会:`C:\\What\\The\\Fxxk`
100+
传统 C++ 里面要编写一个充满特殊字符的字符串其实是非常痛苦的一件事情,
101+
比如一个包含 HTML 本体的字符串需要添加大量的转义符,
102+
例如一个Windows 上的文件路径经常会:`C:\\File\\To\\Path`
97103

98-
C++11 提供了原始字符串字面量的写法,可以在一个字符串前方使用 `R` 来修饰这个字符串,同时,将原始字符串使用括号包裹,例如:
104+
C++11 提供了原始字符串字面量的写法,可以在一个字符串前方使用 `R` 来修饰这个字符串,
105+
同时,将原始字符串使用括号包裹,例如:
99106

100107
```cpp
101108
#include <iostream>
102109
#include <string>
103110

104111
int main() {
105-
std::string str = R"(C:\What\The\Fxxk)";
112+
std::string str = R"(C:\File\To\Path)";
106113
std::cout << str << std::endl;
107114
return 0;
108115
}
@@ -113,7 +120,6 @@ int main() {
113120
C++11 引进了自定义字面量的能力,通过重载双引号后缀运算符实现:
114121

115122
```cpp
116-
117123
// 字符串字面量自定义必须设置如下的参数列表
118124
std::string operator"" _wow1(const char *wow1, size_t len) {
119125
return std::string(wow1)+"woooooooooow, amazing";

code/9/9.1.cpp

Lines changed: 0 additions & 7 deletions
This file was deleted.

code/8/8.1.cpp renamed to code/9/9.1.noexcept.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,18 @@ int main()
3333
try {
3434
may_throw();
3535
} catch (...) {
36-
std::cout << "捕获异常, 来自 my_throw()" << std::endl;
36+
std::cout << "exception captured from my_throw()" << std::endl;
3737
}
3838

3939
try {
4040
non_block_throw();
4141
} catch (...) {
42-
std::cout << "捕获异常, 来自 non_block_throw()" << std::endl;
42+
std::cout << "exception captured from non_block_throw()" << std::endl;
4343
}
4444

4545
try {
4646
block_throw();
4747
} catch (...) {
48-
std::cout << "捕获异常, 来自 block_throw()" << std::endl;
48+
std::cout << "exception captured from block_throw()" << std::endl;
4949
}
5050
}

code/8/8.2.cpp renamed to code/9/9.2.literals.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//
55
// created by changkun at changkun.de
66
//
7-
// 字面量
7+
// literals
88

99
#include <iostream>
1010
#include <string>
@@ -18,16 +18,16 @@ std::string operator""_wow2 (unsigned long long i) {
1818
}
1919

2020
int main() {
21-
std::string str = R"(C:\\What\\The\\Fxxk)";
21+
std::string str = R"(C:\\File\\To\\Path)";
2222
std::cout << str << std::endl;
2323

2424
int value = 0b1001010101010;
2525
std::cout << value << std::endl;
2626

2727

28-
auto str = "abc"_wow1;
28+
auto str2 = "abc"_wow1;
2929
auto num = 1_wow2;
30-
std::cout << str << std::endl;
30+
std::cout << str2 << std::endl;
3131
std::cout << num << std::endl;
3232
return 0;
3333
}

code/9/Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
all: $(patsubst %.cpp, %.out, $(wildcard *.cpp))
2+
3+
%.out: %.cpp Makefile
4+
clang++ $< -o $@ -std=c++2a -pedantic
5+
6+
clean:
7+
rm *.out

0 commit comments

Comments
 (0)