From 84515e559cb6572a8945911f36f9acc9043acba4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CDelta456=E2=80=9D?= Date: Tue, 25 Jun 2019 20:13:20 +0530 Subject: [PATCH 1/4] Begin translation --- book/en-us/00-preface.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 book/en-us/00-preface.md diff --git a/book/en-us/00-preface.md b/book/en-us/00-preface.md new file mode 100644 index 00000000..e69de29b From bf1d37ae5da5c28d77e498f729b613fa3ede60c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CDelta456=E2=80=9D?= Date: Tue, 25 Jun 2019 20:17:07 +0530 Subject: [PATCH 2/4] Begin translation 2 --- book/en-us/00-preface.md | 37 +++++++++++++++++++++++++++++++++++++ code/1/1.1.c.and.cpp | 7 +++++-- code/1/foo.c | 3 ++- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/book/en-us/00-preface.md b/book/en-us/00-preface.md index e69de29b..a6a6246c 100644 --- a/book/en-us/00-preface.md +++ b/book/en-us/00-preface.md @@ -0,0 +1,37 @@ +# Preface + +## Introduction + +C++ is a fairly large langauge which is used by many users around the globe. From the advent of C++98 to the official finalization of C++11, it has accumulated over a decade. C++14/17 is an important addition and also an optimization to C++11. The extended features of all these new standards inject new life into the C++ language. +C++ programmers who are still using C++11/14/17 are still in the process of using **traditional C++** (this book refers to C++98 and its previous C++ features as traditional C++). When it comes to new features such as Lambda expressions, it even reveals the excitement of "not learning the same language." + +**C++1x** or **modern C++** (both refer to C++11/14/17) A number of features are injected into traditional C++ in order to make C++ more like a modern language. C++1x not only enhances the usability of the C++ language itself, but the modifications of the `auto` keyword semantic gives us more confidence in manipulating extremely complex template types. At the same time, a lot of enhancements have been made to the language at runtime. The emergence of Lambda expressions has made C++ have the "closure" feature of "anonymous functions", which is almost used in modern programming languages ​​(such as Python/Swift/..) It has become commonplace, and the emergence of rvalue references that has solved the problem of temporary object efficiency for which C++ was criticized for a long time. + +C++17 is the direction that has been promoted by the C++ community in the past three years. It also points out an important development direction of **Modern C++** programming. Although it does not appear as much as C++11, it contains a large number of small and beautiful languages ​​and features (such as structured binding), and the appearance of these features once again corrects our programming paradigm in C++. + +Modern C++ also adds a lot of tools and methods to its own standard library, such as `std::thread` which is at the level of the language itself, which supports concurrent programming and no longer depends on the underlying system on different platforms. The API implements cross-platform support at the language level; `std::regex` provides full regular expression support and more. C++98 has been proven to be a very successful "paradigm", and the emergence of C++1x further promotes this paradigm, making C++ a better language for system programming and library development. + +All in all, as an advocate and practitioner of C++, we always maintain an open mind to accept new things, and we can promote the development of C++ faster and better, making this old and novel language more vibrant. + +## Target Reader + +1. This book assumes that the reader is already familiar with traditional C++, at least not having difficulty reading traditional C++ code. In other words, those who have long used traditional C++, who are eager to quickly understand the features of **Modern C++** in a short period of time, are well suited to reading this book. + +2. This book introduces some Modern C++ **black magic** to a certain extent, but these magics are limited, and are not suitable for readers who want to learn Modern C++. The positioning of this book is the quick start of **Modern C++**. Of course, readers who want to do advanced learning can use this book to review and test their familiarity with **Modern C++**. + +## Purpose of the book + +This book is called "high-speed start-up". It is a relatively comprehensive introduction to the relevant features of C++ produced before the 1920s. Readers can learn the content of interest according to the following catalogues and learn quickly. What you need to know. These features don't need to be fully mastered. Just learn and review the new features that are right for you based on your own usage needs and specific application scenarios. + +At the same time, in the process of introducing these features, the book introduces the historical background and technical requirements of these features as simple and clear as possible. This book is a great help to understand these features and how to apply them. + +[Back to Contents](./toc.md) | Previous Chapter | [Next Chapter Towards C++11/14/17](./01-intro.md) + +## License + +Creative Commons License Agreement + +This book is originally written by [Ou Changkun](https://github.com/changkun) and translated by [Swastik Baranwal](https://github.com/Delta456) in English, using [Creative Commons](http://creativecommons.org/licenses/by-Nc-nd/4.0/) license. The code in the project is open source using the MIT protocol, see [License](../../LICENSE). + + + diff --git a/code/1/1.1.c.and.cpp b/code/1/1.1.c.and.cpp index 837626ec..03374eff 100644 --- a/code/1/1.1.c.and.cpp +++ b/code/1/1.1.c.and.cpp @@ -11,10 +11,13 @@ #include #include -int main() { +int main() +{ // use lambda expression - [out = std::ref(std::cout << "Result from C code: " << add(1, 2))](){ + [out = std::ref(std::cout << "Result from C code: " << add(1, 2))]() + { out.get() << ".\n"; }(); + return 0; } diff --git a/code/1/foo.c b/code/1/foo.c index 273bdd1c..0fd2574d 100644 --- a/code/1/foo.c +++ b/code/1/foo.c @@ -10,6 +10,7 @@ #include "foo.h" // C code -int add(int x, int y) { +int add(int x, int y) +{ return x+y; } From fba7604cbca40ed4432eb82b323f7b33f1ceecd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CDelta456=E2=80=9D?= Date: Tue, 25 Jun 2019 21:23:36 +0530 Subject: [PATCH 3/4] Fix Grammar Errors and more progress --- CONTRIBUTING-en-us.md | 36 ++++++++++++++++++++++++++++++++++++ README-en-us.md | 9 ++++++--- book/en-us/00-preface.md | 4 ++-- 3 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 CONTRIBUTING-en-us.md diff --git a/CONTRIBUTING-en-us.md b/CONTRIBUTING-en-us.md new file mode 100644 index 00000000..26d101bf --- /dev/null +++ b/CONTRIBUTING-en-us.md @@ -0,0 +1,36 @@ +# Contribution + +[![](https://img.shields.io/badge/chat-community-667ed5.svg)](./assets/community.md) + +## Issue + +C++ 11/14/17 issue is used to track the principled description errors, the existing `typo` errors, and the questions to the author of the book. + +- Usually, you will find typos, semantic errors, grammatical errors, etc in a paragraph of the book. +These are all `typo` errors. If an error has caused some obstacles to your reading and +you strongly believe that the `typo` will also affect others reading. +So you are very welcome to [submit issue](https://github.com/changkun/modern-cpp-tutorial/issues) to report the `typo` error. + +- There are obvious errors in the principle of errors in the book, such as the analysis of the source code, +if the content will be seriously misleading to others, please don't hesitate. +Report this error immediately by [submitting issue](https://github.com/changkun/modern-cpp-tutorial/issues) to avoid the spread of wrong knowledge. + +Please also include relevant improvements if you can. In general, if there is such a problem, we encourage you to submit an improvement PR. + +## Pull request + +"C++ 11/14/17" is an open source book that anyone can contribute to contribute to PR. However, please read the following instructions carefully before submitting PR: + +- When you think you need to submit a PR, make sure that the [issue list](https://github.com/changkun/modern-cpp-tutorial/issues) already contains the problem you want to solve. +If not, please refer to the **Submit Issue** section, submit your issue, and submit your PR. + +- When you are ready to submit a typo error PR, make sure your PR has improved the `typo` error of more than 50 Chinese characters (or English words)**, otherwise please do not submit PR. +- For a PR that fixes principled errors, please don't hesitate, I am very grateful for this! +- If you really like this book, and you want to be a co-author of this book, become an author, please send an email to ask: `hi at changkun dot us`. + +This repository provides a variety of reading methods. If you submit a Pull request, make sure you check the following checklist: + +- [ ] Only change part of the original book text `book` which does not involve the modification of the code snippet, no need to modify +- [ ] If you also change the code snippet in the body, you need to synchronize the corresponding code snippet in the `code` folder. +- [ ] If the changes also involve the design of the problem, you need to synchronize the contents of the `exercises` folder. + diff --git a/README-en-us.md b/README-en-us.md index 495ef8e4..3a1079d0 100644 --- a/README-en-us.md +++ b/README-en-us.md @@ -19,7 +19,7 @@ In addition, The author would like to encourage that readers should be able to u ## Targets -- This book assumes that readers are already familiar with traditional C++ (e.g. C++98), at least they do not have any difficulty in reading traditional C++ code. In other words, those who have long experience in traditional C++ and people who desire to quickly understand the features of modern C++ in a short period of time are well suited to read the book; +- This book assumes that readers are already familiar with traditional C++ (i.e. C++98 or lower), at least they do not have any difficulty in reading traditional C++ code. In other words, those who have long experience in traditional C++ and people who desire to quickly understand the features of modern C++ in a short period of time are well suited to read the book; - This book introduces to a certain extent of the dark magic of modern C++. However, these magics are very limited, they are not suitable for readers who want to learn advanced C++. The purpose of this book is offering a quick start for modern C++. Of course, advanced readers can also use this book to review and examine themselves on modern C++. @@ -45,9 +45,12 @@ The source code of the [website](https://changkun.de/modern-cpp) of this book ca ## Acknowledgements -This book is originally written in Chinese. +This book was originally made by [Ou Changkun](https://github.com/changkun) in Chinese and is being translated by [Swastik Baranwal](https://github.com/Delta456) -The author has limited time and language skills. If readers find any mistakes of the book or any language improvements, please feel free to open an [Issue](https://github.com/changkun/modern-cpp-tutorial/issues) or start a [Pull request](https://github.com/changkun/modern-cpp-tutorial/pulls). +- [Issue](https://github.com/changkun/modern-cpp-tutorial/issues) +- [Pull request](https://github.com/changkun/modern-cpp-tutorial/pulls) +- [Contributing](CONTRIBUTING-en-us.md) +- [Contributors](https://github.com/changkun/modern-cpp-tutorial/graphs/contributors) ## Licenses diff --git a/book/en-us/00-preface.md b/book/en-us/00-preface.md index a6a6246c..0955bb5e 100644 --- a/book/en-us/00-preface.md +++ b/book/en-us/00-preface.md @@ -13,7 +13,7 @@ Modern C++ also adds a lot of tools and methods to its own standard library, suc All in all, as an advocate and practitioner of C++, we always maintain an open mind to accept new things, and we can promote the development of C++ faster and better, making this old and novel language more vibrant. -## Target Reader +## Targeted Readers 1. This book assumes that the reader is already familiar with traditional C++, at least not having difficulty reading traditional C++ code. In other words, those who have long used traditional C++, who are eager to quickly understand the features of **Modern C++** in a short period of time, are well suited to reading this book. @@ -31,7 +31,7 @@ At the same time, in the process of introducing these features, the book introdu Creative Commons License Agreement -This book is originally written by [Ou Changkun](https://github.com/changkun) and translated by [Swastik Baranwal](https://github.com/Delta456) in English, using [Creative Commons](http://creativecommons.org/licenses/by-Nc-nd/4.0/) license. The code in the project is open source using the MIT protocol, see [License](../../LICENSE). +This book is originally written by [Ou Changkun](https://github.com/changkun) in Chinese and translated by [Swastik Baranwal](https://github.com/Delta456) in English, using [Creative Commons](http://creativecommons.org/licenses/by-Nc-nd/4.0/) license. The code in the project is open source using the MIT protocol, see [License](../../LICENSE). From 7180e6a1668fa96b9c07d34a41aa407832cdf585 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CDelta456=E2=80=9D?= Date: Wed, 26 Jun 2019 11:34:46 +0530 Subject: [PATCH 4/4] First chapter done --- book/en-us/00-preface.md | 2 +- book/en-us/01-intro.md | 149 +++++++++++++++++++++++++++++++++++++++ book/zh-cn/01-intro.md | 13 ++-- 3 files changed, 159 insertions(+), 5 deletions(-) diff --git a/book/en-us/00-preface.md b/book/en-us/00-preface.md index 0955bb5e..2dc2a5bc 100644 --- a/book/en-us/00-preface.md +++ b/book/en-us/00-preface.md @@ -25,7 +25,7 @@ This book is called "high-speed start-up". It is a relatively comprehensive intr At the same time, in the process of introducing these features, the book introduces the historical background and technical requirements of these features as simple and clear as possible. This book is a great help to understand these features and how to apply them. -[Back to Contents](./toc.md) | Previous Chapter | [Next Chapter Towards C++11/14/17](./01-intro.md) +[Back to Contents](./toc.md) | Previous Chapter | [Next Chapter](./01-intro.md) ## License diff --git a/book/en-us/01-intro.md b/book/en-us/01-intro.md index e69de29b..221e5829 100644 --- a/book/en-us/01-intro.md +++ b/book/en-us/01-intro.md @@ -0,0 +1,149 @@ +--- +Title: Chapter 1 Towards C++11/14/17 +Type: book-en-us +Order: 1 +--- + +# Chapter 1 Towards C++11/14/17 + +[TOC] + +**Compilation Environment**: This book will use `clang++` as the only compiler used, and always use the `-std=c++17` as compilation flag in your code. + +```bash +→ clang++ -v +Apple LLVM version 9.1.0 (clang-902.0.39.1) +Target: x86_64-apple-darwin17.5.0 +Thread model: posix +InstalledDir: /Library/Developer/CommandLineTools/usr/bin +``` + +## 1.1 Deprecated Features + +Before learning C++1x, let's take a look at the main features that have been deprecated since C++11: + +> **Note**: Deprecation is not completely unusable, it is only intended to imply that programmers will disappear from future standards and should be avoided. However, the deprecated features are still part of the standard library, and most of the features are actually "permanently" reserved for compatibility reasons. + +- **The string literal constant is no longer allowed to be assigned to a `char *`. If you need to assign and initialize a `char *` with a string literal constant, you should use `const char *` or `auto`.** +```cpp + char *str = "hello world!"; // A deprecation warning will appear +``` +- **C++98 exception description, `unexpected_handler`, `set_unexpected()` and other related features are deprecated and should use `noexcept`.** + +- **`auto_ptr` is deprecated and `unique_ptr` should be used.** + +- **`register` keyword is deprecated and can be used but no longer has any practical meaning.** + +- The `++` operation of the **`bool` type is deprecated.** + +- **If a class has a destructor, the properties for which it generates copy constructors and copy assignment operators are deprecated.** + +- **C language style type conversion is deprecated (i.e. using `(convert_type)`) before variables, and `static_cast`, `reinterpret_cast`, `const_cast` should be used for type conversion. ** + +- **In particular, some of the C standard libraries that can be used are deprecated in the latest C++17 standard, such as ``, ``, `` and ``** + +- ……and many more + +There are also other features such as parameter binding (C++11 provides `std::bind` and `std::function`), `export`, etc. are also deprecated. These features mentioned above** If you have never used or heard of it, please don't try to understand them. You should move closer to the new standard and learn new features directly. After all, technology is moving forward. + +## 1.2 Compatibility with C + +For some force majeure and historical reasons, we had to use some C code (even old C code) in C++, such as Linux system calls. Before the advent of C++1x, most people talked about "what is the difference between C and C++". Generally speaking, in addition to answering object-oriented class features and template features of generic programming, there is no other opinion, even Directly answering "almost" is also a big one. The Wayne diagram in Figure 1.2 roughly answers the C and C++ related compatibility. + +![Figure 1.2: C and C++ compatibility](../../assets/figures/comparison.png) + +From now on, you should have the idea that "**C++ is not a superset of C**" in your mind (and not from the beginning, later [References for further reading](# further reading references) The difference between C++98 and C99 is given). When writing C++, you should also avoid using programming styles such as `void*` whenever possible. When you have to use C, you should pay attention to the use of `extern "C"`, separate the C code from the C++ code, and then unify the link, for example: + +```cpp +// foo.h +#ifdef __cplusplus +extern "C" { +#endif + +int add(int x, int y); + +#ifdef __cplusplus +} +#endif + +// foo.c +int add(int x, int y) +{ + return x+y; +} + +// 1.1.cpp +#include "foo.h" +#include +#include + +int main() +{ + [out = std::ref(std::cout << "Result from C code: " << add(1, 2))]() + { + out.get() << ".\n"; + }(); + + return 0; +} +``` +You should first compile the C code with `gcc`: + +```bash +gcc -c foo.c +``` + +Compile the foo.o file and link the C++ code to the `.o` file using `clang++` (or both compile to `.o` and then unlink): + +```bash +clang++ 1.1.cpp foo.o -std=c++17 -o 1.1 +``` + +Of course, you can use `Makefile` to compile the above code: + +```makefile +C = gcc +CXX = clang++ + +SOURCE_C = foo.c +OBJECTS_C = foo.o + +SOURCE_CXX = 1.1.cpp + +TARGET = 1.1 +LDFLAGS_COMMON = -std=c++17 + +all: + $(C) -c $(SOURCE_C) + $(CXX) $(SOURCE_CXX) $(OBJECTS_C) $(LDFLAGS_COMMON) -o $(TARGET) +clean: + rm -rf *.o $(TARGET) +``` +> Note: Indentation in Makefile is a tab instead of a space character. If you copy this code directly into your editor, the tab may automatically be replaced. Please ensure the indentation in Makefile is done by tabs only. + +> If you don't know the how to use Makefile. In this tutorial, you don't have to build complicated codes. You can also read this book by simply using `clang++ -std=c++17` on the command line. + +If you are new to modern C++, you probably still won't understand the small piece of code above, namely: + +```cpp +[out = std::ref(std::cout << "Result from C code: " << add(1, 2))]() +{ + out.get() << ".\n"; +}(); +``` +Don't worry, the following chapters in this book will explain the above code. + +[Back to Contents](./toc.md)| [Previous Chapter](./00-preface.md) | [Next Chapter](./01-intro.md) + +## Further reading + +1. [C++ Language Guide by Bjarne Stroustrup](https://www.amazon.com/Tour-2nd-Depth-Bjarne-Stroustrup/dp/0134997832) +2. [History of C++ ](http://en.cppreference.com/w/cpp/language/history) +3. [Support for C++ 1x features in compilers such as GCC/Clang](http://en.cppreference.com/w/cpp/compiler_support) +4. [The difference between C++98 and C99](http://david.tribble.com/text/cdiffs.htm#C99-vs-CPP98) + +## License + +Creative Commons License Agreement + +This book is originally written by [Ou Changkun](https://github.com/changkun) in Chinese and is translated by [Swastik Baranwal](https://github.com/Delta456) in English, using [Creative Commons](http://creativecommons.org/licenses/by-Nc-nd/4.0/) license. The code in the project is open source using the MIT protocol, see [License](../../LICENSE). diff --git a/book/zh-cn/01-intro.md b/book/zh-cn/01-intro.md index cc2cb566..74302a2d 100644 --- a/book/zh-cn/01-intro.md +++ b/book/zh-cn/01-intro.md @@ -68,7 +68,8 @@ int add(int x, int y); #endif // foo.c -int add(int x, int y) { +int add(int x, int y) +{ return x+y; } @@ -77,10 +78,13 @@ int add(int x, int y) { #include #include -int main() { - [out = std::ref(std::cout << "Result from C code: " << add(1, 2))](){ +int main() +{ + [out = std::ref(std::cout << "Result from C code: " << add(1, 2))]() + { out.get() << ".\n"; }(); + return 0; } ``` @@ -125,7 +129,8 @@ clean: 如果你是首次接触现代 C++,那么你很可能还看不懂上面的那一小段代码,即: ```cpp -[out = std::ref(std::cout << "Result from C code: " << add(1, 2))](){ +[out = std::ref(std::cout << "Result from C code: " << add(1, 2))]() +{ out.get() << ".\n"; }(); ```