Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1st chapter done #69

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions CONTRIBUTING-en-us.md
Original file line number Diff line number Diff line change
@@ -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.

9 changes: 6 additions & 3 deletions README-en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -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++.

Expand All @@ -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

Expand Down
37 changes: 37 additions & 0 deletions book/en-us/00-preface.md
Original file line number Diff line number Diff line change
@@ -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.

## 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.

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](./01-intro.md)

## License

<a rel="license" href="http://creativecommons.org/licenses/by-nc-nd/4.0/"><img alt="Creative Commons License Agreement" style="border-width:0" src ="https://i.creativecommons.org/l/by-nc-nd/4.0/80x15.png" /></a>

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



149 changes: 149 additions & 0 deletions book/en-us/01-intro.md
Original file line number Diff line number Diff line change
@@ -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 `<ccomplex>`, `<cstdalign>`, `<cstdbool>` and `<ctgmath>`**

- ……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 <iostream>
#include <functional>

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

<a rel="license" href="http://creativecommons.org/licenses/by-nc-nd/4.0/"><img alt="Creative Commons License Agreement" style="border-width:0" src ="https://i.creativecommons.org/l/by-nc-nd/4.0/80x15.png" /></a>

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).
13 changes: 9 additions & 4 deletions book/zh-cn/01-intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -77,10 +78,13 @@ int add(int x, int y) {
#include <iostream>
#include <functional>

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;
}
```
Expand Down Expand Up @@ -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";
}();
```
Expand Down
7 changes: 5 additions & 2 deletions code/1/1.1.c.and.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
#include <iostream>
#include <functional>

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;
}
3 changes: 2 additions & 1 deletion code/1/foo.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "foo.h"

// C code
int add(int x, int y) {
int add(int x, int y)
{
return x+y;
}