Convert Figma logo to code with AI

changkun logomodern-cpp-tutorial

📚 Modern C++ Tutorial: C++11/14/17/20 On the Fly | https://changkun.de/modern-cpp/

23,821
2,976
23,821
18

Top Related Projects

6,118

Guidelines Support Library

The C++ Core Guidelines are a set of tried-and-true guidelines, rules, and best practices about coding in C++

A cheatsheet of modern C++ language and library features.

A collection of resources on modern C++

Collaborative Collection of C++ Best Practices. This online resource is part of Jason Turner's collection of C++ Best Practices resources. See README.md for more information.

Quick Overview

The "Modern C++ Tutorial" is an open-source book and accompanying code repository that aims to introduce C++11/14/17/20 features to programmers. It provides a comprehensive guide to modern C++ programming techniques, covering new language features, best practices, and practical examples.

Pros

  • Covers a wide range of modern C++ features from C++11 to C++20
  • Includes both English and Chinese versions of the tutorial
  • Provides practical code examples and explanations
  • Regularly updated to include the latest C++ standards

Cons

  • May be overwhelming for complete beginners to C++
  • Some advanced topics might require additional resources for in-depth understanding
  • The repository structure could be more organized for easier navigation
  • Some sections may need more detailed explanations for complex concepts

Code Examples

Here are a few code examples from the tutorial:

  1. Lambda expressions (C++11):
auto add = [](int a, int b) { return a + b; };
std::cout << add(1, 2) << std::endl;  // Output: 3
  1. Range-based for loop (C++11):
std::vector<int> vec = {1, 2, 3, 4, 5};
for (const auto &i : vec) {
    std::cout << i << " ";
}
// Output: 1 2 3 4 5
  1. Structured bindings (C++17):
std::tuple<int, double, std::string> t(1, 2.3, "456");
auto [x, y, z] = t;
std::cout << x << ", " << y << ", " << z << std::endl;
// Output: 1, 2.3, 456

Getting Started

To get started with the Modern C++ Tutorial:

  1. Clone the repository:

    git clone https://github.com/changkun/modern-cpp-tutorial.git
    
  2. Navigate to the book directory for the tutorial content.

  3. For code examples, check the code directory.

  4. To build the book locally, follow the instructions in the README.md file.

  5. To compile and run code examples, use a modern C++ compiler (e.g., GCC 7+ or Clang 5+) with C++17 support:

    g++ -std=c++17 example.cpp -o example
    ./example
    

Competitor Comparisons

6,118

Guidelines Support Library

Pros of GSL

  • Provides a set of standardized C++ core guidelines support libraries
  • Backed by Microsoft, ensuring high-quality and well-maintained code
  • Focuses on improving safety, reliability, and performance in C++ programming

Cons of GSL

  • Steeper learning curve for beginners compared to Modern C++ Tutorial
  • Less comprehensive coverage of modern C++ features and concepts
  • Requires more advanced C++ knowledge to fully utilize and implement

Code Comparison

GSL:

#include <gsl/gsl>

void example(gsl::span<int> arr) {
    for (int& i : arr) {
        i *= 2;
    }
}

Modern C++ Tutorial:

#include <vector>
#include <algorithm>

void example(std::vector<int>& arr) {
    std::for_each(arr.begin(), arr.end(), [](int& i) {
        i *= 2;
    });
}

The GSL example uses the gsl::span type for safer array access, while the Modern C++ Tutorial example demonstrates the use of standard library containers and algorithms.

The C++ Core Guidelines are a set of tried-and-true guidelines, rules, and best practices about coding in C++

Pros of CppCoreGuidelines

  • Comprehensive set of guidelines for writing modern C++ code
  • Backed by the C++ Standards Committee, ensuring high-quality and up-to-date information
  • Covers a wide range of topics, from basic syntax to advanced concepts

Cons of CppCoreGuidelines

  • Can be overwhelming for beginners due to its extensive coverage
  • Lacks practical examples and hands-on tutorials
  • Focuses more on best practices rather than teaching language features

Code Comparison

CppCoreGuidelines example (Rule C.47):

class Vector {
public:
    Vector(int s);
    ~Vector() { delete[] elem; }
    // ...
private:
    double* elem;
    int sz;
};

modern-cpp-tutorial example (Chapter 2.6):

class Foo {
public:
    int value() const;
    void set_value(int v);
private:
    int value_;
};

CppCoreGuidelines provides more detailed explanations of best practices, while modern-cpp-tutorial offers simpler examples to introduce language features. The CppCoreGuidelines example demonstrates proper resource management, whereas the modern-cpp-tutorial example focuses on basic class structure and encapsulation.

A cheatsheet of modern C++ language and library features.

Pros of modern-cpp-features

  • Concise and focused on specific C++ features
  • Regularly updated with the latest C++ standards
  • Well-organized with clear categorization of features

Cons of modern-cpp-features

  • Less comprehensive tutorial-style content
  • Fewer explanations and examples for each feature
  • Limited coverage of broader C++ concepts and best practices

Code Comparison

modern-cpp-features:

// C++17 structured bindings
auto [x, y, z] = std::tuple{1, 2, 3};

modern-cpp-tutorial:

// C++17 structured bindings with explanation
auto [x, y, z] = std::tuple{1, 2, 3};
// This allows us to unpack the tuple into individual variables
std::cout << x << y << z << std::endl; // Outputs: 123

The modern-cpp-features repository provides a more concise overview of C++ features, while modern-cpp-tutorial offers more detailed explanations and examples. modern-cpp-features is better suited for quick reference, whereas modern-cpp-tutorial is more appropriate for in-depth learning and understanding of modern C++ concepts.

A collection of resources on modern C++

Pros of awesome-modern-cpp

  • Comprehensive collection of resources covering various aspects of modern C++
  • Regularly updated with new links and resources
  • Well-organized into categories for easy navigation

Cons of awesome-modern-cpp

  • Lacks in-depth explanations or tutorials
  • May be overwhelming for beginners due to the sheer volume of resources

Code comparison

modern-cpp-tutorial:

auto add = [](int x, int y) { return x + y; };
std::cout << add(1, 2) << std::endl;

awesome-modern-cpp: No direct code examples provided, as it's primarily a curated list of resources.

Summary

modern-cpp-tutorial offers a structured learning path with detailed explanations and code examples, making it ideal for beginners and those looking for a comprehensive tutorial. awesome-modern-cpp, on the other hand, serves as an extensive reference guide, providing a wide range of resources for various aspects of modern C++. While it may not offer direct tutorials, it's an excellent resource for finding specific information and staying up-to-date with the latest developments in the C++ ecosystem.

Both repositories have their merits, and developers may find value in using them complementarily. modern-cpp-tutorial can provide the foundational knowledge, while awesome-modern-cpp can be used for further exploration and deepening one's understanding of specific topics in modern C++.

Collaborative Collection of C++ Best Practices. This online resource is part of Jason Turner's collection of C++ Best Practices resources. See README.md for more information.

Pros of cppbestpractices

  • Comprehensive coverage of C++ best practices, including coding style, design patterns, and performance optimization
  • Regularly updated with community contributions, reflecting current industry standards
  • Includes sections on tooling, testing, and project management

Cons of cppbestpractices

  • Less structured learning path compared to modern-cpp-tutorial
  • Lacks in-depth explanations of modern C++ features
  • No hands-on examples or exercises for practical application

Code Comparison

modern-cpp-tutorial:

auto add = [](int a, int b) { return a + b; };
std::cout << add(1, 2) << std::endl;

cppbestpractices:

// No specific code examples provided, as it focuses on general guidelines rather than code snippets

The modern-cpp-tutorial repository provides more concrete code examples to illustrate modern C++ features, while cppbestpractices focuses on high-level guidelines and best practices without specific code implementations.

Both repositories serve different purposes: modern-cpp-tutorial is a structured tutorial for learning modern C++ features, while cppbestpractices is a comprehensive reference for writing high-quality C++ code. Developers may find value in using both resources complementarily to improve their C++ skills and knowledge.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

logo

Modern C++ Tutorial: C++11/14/17/20 On the Fly

Purpose

The book claims to be "On the Fly". Its intent is to provide a comprehensive introduction to the relevant features regarding modern C++ (before 2020s). Readers can choose interesting content according to the following table of content to learn and quickly familiarize the new features you would like to learn. Readers should be aware that not all of these features are required. Instead, it should be learned when you really need it.

At the same time, instead of coding-only, the book introduces the historical background of its technical requirements (as simple as possible), which provides great help in understanding why these features came out.

In addition, the author would like to encourage readers to use modern C++ directly in their new projects and migrate their old projects to modern C++ gradually after reading the book.

Targets

  • This book assumes that readers are already familiar with traditional C++ (i.e. C++98 or earlier), or at least that 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, the dark magic of modern C++. However, these magic tricks 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++.

Start

You can choose from the following reading methods:

Code

Each chapter of this book contains a lot of code. If you encounter problems while writing your own code with the introductory features of the book, reading the source code attached to the book might be of help. You can find the book here. All the code is organized by chapter, the folder name is the chapter number.

Exercises

There are few exercises at the end of each chapter of the book. These are meant to test whether you have mastered the knowledge in the current chapter. You can find the possible answer to the problem here. Again, the folder name is the chapter number.

Website

The source code of the website of this book can be found here, which is built by hexo and vuejs. The website provides you another way of reading the book, it also adapts to mobile browsers.

Build

If you are interested in building everything locally, it is recommended using Docker. To build, simply run:

$ make build

Acknowledgements

This book was originally written in Chinese by Changkun Ou.

The author has limited time and language skills. If readers find any mistakes in the book or any language improvements, please feel free to open an Issue or start a Pull request. For detailed guidelines and checklist, please refer to How to contribute.

The author is grateful to all contributors, including but not limited to Contributors.

This project is also supported by:

Licenses

Creative Commons License
This work was written by Ou Changkun and licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License. The code of this repository is open sourced under the MIT license.