From 9779283735d6993add386f9749d2cb354340d7df Mon Sep 17 00:00:00 2001 From: Changkun Ou Date: Sun, 14 Jul 2019 23:10:07 +0200 Subject: [PATCH] see #12: translate ch09 --- book/en-us/09-others.md | 163 ++++++++++++++++++++++++- book/zh-cn/09-others.md | 22 ++-- code/9/9.1.cpp | 7 -- code/{8/8.1.cpp => 9/9.1.noexcept.cpp} | 6 +- code/{8/8.2.cpp => 9/9.2.literals.cpp} | 8 +- code/9/Makefile | 7 ++ 6 files changed, 189 insertions(+), 24 deletions(-) delete mode 100644 code/9/9.1.cpp rename code/{8/8.1.cpp => 9/9.1.noexcept.cpp} (79%) rename code/{8/8.2.cpp => 9/9.2.literals.cpp} (81%) create mode 100644 code/9/Makefile diff --git a/book/en-us/09-others.md b/book/en-us/09-others.md index 6f780055..31c6c97e 100644 --- a/book/en-us/09-others.md +++ b/book/en-us/09-others.md @@ -6,9 +6,168 @@ order: 9 # Chapter 09 Minor Features -[Table of Content](./toc.md) | [Previous Chapter](./08-filesystem.md) | [Next Chapter: Outlook: Introduction of C++20](./10-cpp20.md) +[TOC] + +## 9.1 New Type + +### `long long int` + +`long long int` is not the first to be introduced in C++11. +In fact, as early as C99, `long long int` has been included in the C standard, +so most compilers already support it. +C++11 now formally incorporate it into the standard library, +specifying a `long long int` type with at least 64 bits. + +## 9.2 `noexcept` Operations + +One of the big advantages of C++ over C is that +C++ itself defines a complete set of exception handling mechanisms. +However, before C++11, almost no one used to write +an exception declaration expression after the function name. +Starting from C++11, this mechanism was deprecated, +so we will not discuss or introduce the previous mechanism. +How to work and how to use it, you should not take the initiative +to understand it. + +C++11 simplifies exception declarations into two cases: + +1. The function may throw any exceptions +2. The function can't throw any exceptions + +And use `noexcept` to limit these two behaviors, for example: + +```cpp +void may_throw(); // May throw any exception +void no_throw() noexcept; // Cannot throw any exception +``` + +If a function modified with `noexcept` is thrown, +the compiler will use `std::terminate()` to +immediately terminate the program. + +`noexcept` can also be used as an operator to manipulate an expression. +When the expression has no exception, it returns `true`, +otherwise it returns `false`. + +```cpp +#include +void may_throw() { + throw true; +} +auto non_block_throw = []{ + may_throw(); +}; +void no_throw() noexcept { + return; +} + +auto block_throw = []() noexcept { + no_throw(); +}; +int main() +{ + std::cout << std::boolalpha + << "may_throw() noexcept? " << noexcept(may_throw()) << std::endl + << "no_throw() noexcept? " << noexcept(no_throw()) << std::endl + << "lmay_throw() noexcept? " << noexcept(non_block_throw()) << std::endl + << "lno_throw() noexcept? " << noexcept(block_throw()) << std::endl; + return 0; +} +``` + +`noexcept` can modify the function of blocking exceptions +after modifying a function. If an exception is generated internally, +the external will not trigger. For instance: + +```cpp +try { + may_throw(); +} catch (...) { + std::cout << "exception captured from my_throw()" << std::endl; +} +try { + non_block_throw(); +} catch (...) { + std::cout << "exception captured from non_block_throw()" << std::endl; +} +try { + block_throw(); +} catch (...) { + std::cout << "exception captured from block_throw()" << std::endl; +} +``` + +The final output is: -## Further Readings +``` +exception captured, from my_throw() +exception captured, from non_block_throw() +``` + +## 9.3 Literal + +### String Literal + +In traditional C++, it is very painful to write a string full of +special characters. For example, a string containing HTML ontology +needs to add a large number of escape characters. +For example, a file path on Windows often as: `C:\\Path\\To\\File`. + +C++11 provides the original string literals, +which can be decorated with `R` in front of a string, +and the original string is wrapped in parentheses, for example: + +```cpp +#include +#include + +int main() { + std::string str = R"(C:\Path\To\File)"; + std::cout << str << std::endl; + return 0; +} +``` + +### Custom Literal + +C++11 introduces the ability to customize literals by +overloading the double quotes suffix operator: + +```cpp +// String literal customization must be set to the following parameter list +std::string operator"" _wow1(const char *wow1, size_t len) { + return std::string(wow1)+"woooooooooow, amazing"; +} + +std::string operator"" _wow2 (unsigned long long i) { + return std::to_string(i)+"woooooooooow, amazing"; +} + +int main() { + auto str = "abc"_wow1; + auto num = 1_wow2; + std::cout << str << std::endl; + std::cout << num << std::endl; + return 0; +} +``` + +Custom literals support four literals: + +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; +2. Floating-point literals: You must use `long double`, `const char *`, and template literals when overloading; +3. String literals: A parameter table of the form `(const char *, size_t)` must be used; +4. Character literals: Parameters can only be `char`, `wchar_t`, `char16_t`, `char32_t`. + +## Conclusion + +Several of the features introduced in this section are those that +use more frequent features from modern C++ features that +have not yet been introduced. `noexcept` is the most important feature. +One of its features is to prevent the spread of anomalies, +effective Let the compiler optimize our code to the maximum extent possible. + +[Table of Content](./toc.md) | [Previous Chapter](./08-filesystem.md) | [Next Chapter: Outlook: Introduction of C++20](./10-cpp20.md) ## Licenses diff --git a/book/zh-cn/09-others.md b/book/zh-cn/09-others.md index 5e4573c7..94e6d978 100644 --- a/book/zh-cn/09-others.md +++ b/book/zh-cn/09-others.md @@ -6,19 +6,23 @@ order: 9 # 第 9 章 其他杂项 -> 内容修订中 - [TOC] ## 9.1 新类型 ### `long long int` -`long long int` 并不是 C++11 最先引入的,其实早在 C99,`long long int` 就已经被纳入 C 标准中,所以大部分的编译器早已支持。C++11 的工作则是正式把它纳入标准库,规定了一个 `long long int` 类型至少具备 64 位的比特数。 +`long long int` 并不是 C++11 最先引入的,其实早在 C99, +`long long int` 就已经被纳入 C 标准中,所以大部分的编译器早已支持。 +C++11 的工作则是正式把它纳入标准库, +规定了一个 `long long int` 类型至少具备 64 位的比特数。 ## 9.2 noexcept 的修饰和操作 -C++ 相比于 C 的一大优势就在于 C++ 本身就定义了一套完整的异常处理机制。然而在 C++11 之前,几乎没有人去使用在函数名后书写异常声明表达式,从 C++11 开始,这套机制被弃用,所以我们不去讨论也不去介绍以前这套机制是如何工作如何使用,你更不应该主动去了解它。 +C++ 相比于 C 的一大优势就在于 C++ 本身就定义了一套完整的异常处理机制。 +然而在 C++11 之前,几乎没有人去使用在函数名后书写异常声明表达式, +从 C++11 开始,这套机制被弃用,所以我们不去讨论也不去介绍以前这套机制是如何工作如何使用, +你更不应该主动去了解它。 C++11 将异常的声明简化为以下两种情况: @@ -93,16 +97,19 @@ try { ### 原始字符串字面量 -传统 C++ 里面要编写一个充满特殊字符的字符串其实是非常痛苦的一件事情,比如一个包含 HTML 本体的字符串需要添加大量的转义符,例如一个Windows 上的文件路径经常会:`C:\\What\\The\\Fxxk`。 +传统 C++ 里面要编写一个充满特殊字符的字符串其实是非常痛苦的一件事情, +比如一个包含 HTML 本体的字符串需要添加大量的转义符, +例如一个Windows 上的文件路径经常会:`C:\\File\\To\\Path`。 -C++11 提供了原始字符串字面量的写法,可以在一个字符串前方使用 `R` 来修饰这个字符串,同时,将原始字符串使用括号包裹,例如: +C++11 提供了原始字符串字面量的写法,可以在一个字符串前方使用 `R` 来修饰这个字符串, +同时,将原始字符串使用括号包裹,例如: ```cpp #include #include int main() { - std::string str = R"(C:\What\The\Fxxk)"; + std::string str = R"(C:\File\To\Path)"; std::cout << str << std::endl; return 0; } @@ -113,7 +120,6 @@ int main() { C++11 引进了自定义字面量的能力,通过重载双引号后缀运算符实现: ```cpp - // 字符串字面量自定义必须设置如下的参数列表 std::string operator"" _wow1(const char *wow1, size_t len) { return std::string(wow1)+"woooooooooow, amazing"; diff --git a/code/9/9.1.cpp b/code/9/9.1.cpp deleted file mode 100644 index a791a11e..00000000 --- a/code/9/9.1.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include -#include -int main() { - std::list l = {1, 2, 3}; - std::sort(l.begin(), l.end()); - return 0; -} diff --git a/code/8/8.1.cpp b/code/9/9.1.noexcept.cpp similarity index 79% rename from code/8/8.1.cpp rename to code/9/9.1.noexcept.cpp index 1a07b216..c5b799af 100644 --- a/code/8/8.1.cpp +++ b/code/9/9.1.noexcept.cpp @@ -33,18 +33,18 @@ int main() try { may_throw(); } catch (...) { - std::cout << "捕获异常, 来自 my_throw()" << std::endl; + std::cout << "exception captured from my_throw()" << std::endl; } try { non_block_throw(); } catch (...) { - std::cout << "捕获异常, 来自 non_block_throw()" << std::endl; + std::cout << "exception captured from non_block_throw()" << std::endl; } try { block_throw(); } catch (...) { - std::cout << "捕获异常, 来自 block_throw()" << std::endl; + std::cout << "exception captured from block_throw()" << std::endl; } } diff --git a/code/8/8.2.cpp b/code/9/9.2.literals.cpp similarity index 81% rename from code/8/8.2.cpp rename to code/9/9.2.literals.cpp index 294e0241..3dee477a 100644 --- a/code/8/8.2.cpp +++ b/code/9/9.2.literals.cpp @@ -4,7 +4,7 @@ // // created by changkun at changkun.de // -// 字面量 +// literals #include #include @@ -18,16 +18,16 @@ std::string operator""_wow2 (unsigned long long i) { } int main() { - std::string str = R"(C:\\What\\The\\Fxxk)"; + std::string str = R"(C:\\File\\To\\Path)"; std::cout << str << std::endl; int value = 0b1001010101010; std::cout << value << std::endl; - auto str = "abc"_wow1; + auto str2 = "abc"_wow1; auto num = 1_wow2; - std::cout << str << std::endl; + std::cout << str2 << std::endl; std::cout << num << std::endl; return 0; } diff --git a/code/9/Makefile b/code/9/Makefile new file mode 100644 index 00000000..fe333edc --- /dev/null +++ b/code/9/Makefile @@ -0,0 +1,7 @@ +all: $(patsubst %.cpp, %.out, $(wildcard *.cpp)) + +%.out: %.cpp Makefile + clang++ $< -o $@ -std=c++2a -pedantic + +clean: + rm *.out \ No newline at end of file