Quay lại Khóa học
AI013 Professional

Modern C++ Tutorial

This tutorial aims to provide experienced developers with a quick reference to the new features of C++11/14/17/20. It covers language usability enhancements, runtime improvements, new containers, smart pointers and memory management, regular expressions, concurrent programming, and a preview of C++20.

4.8
30h
318 học viên
0 lượt thích
Trí tuệ nhân tạo

Tổng quan khóa học

📚 Content Summary

This tutorial is designed to provide experienced developers with a quick reference to the new features of C++11/14/17/20. The content covers language usability enhancements, runtime improvements, the use of new containers, smart pointers and memory management, regular expressions, concurrent programming, and a preview of C++20.

Quickly get up to speed with C++11/14/17/20 and step through the gateway to modern C++ programming.

Author: 欧长坤 (Ou Changkun)

Acknowledgments: 采用 CC BY-NC-ND 4.0 协议授权,致谢所有 GitHub 贡献者及相关社区支持。

🎯 Learning Objectives

  1. Identify and verify the C++ compiler environment and target architecture using command-line tools.
  2. Explain the standard compatibility and overlapping history between C (C89, C99, C11) and C++ (C++98, C++1x).
  3. Implement functional cross-language linking by using extern "C" headers and multi-stage compilation workflows.
  4. Resolve pointer ambiguity using nullptr and implement compile-time logic using constexpr and if constexpr.
  5. Simplify complex declarations using auto, decltype, and type aliasing (using) while ensuring robust object-oriented design with override and final.
  6. Design highly generic code using variadic templates, fold expressions, and non-type template parameter deduction.
  7. Define and implement Lambda expressions using various capture modes (Value, Reference, Expression, and Generic).
  8. Utilize std::function and std::bind to create flexible function wrappers and partial function applications.
  9. Categorize expressions into Lvalues, Prvalues, and Xvalues, and apply Move Semantics to optimize class resource management.
  10. Differentiate between the memory models and use cases of std::array (fixed size, stack) and std::vector (dynamic size, heap).

🔹 Lesson 1: C++ Historical Evolution and Environment

Overview: This lesson covers the technical foundation of the C++ development environment and its historical relationship with the C language. It details how C++ standards (C++98 to C++1x) overlap with C standards (C89 to C11) and provides the specific mechanisms, such as extern "C", required to successfully compile and link mixed C and C++ codebases.

Learning Outcomes:

  • Identify and verify the C++ compiler environment and target architecture using command-line tools.
  • Explain the standard compatibility and overlapping history between C (C89, C99, C11) and C++ (C++98, C++1x).
  • Implement functional cross-language linking by using extern "C" headers and multi-stage compilation workflows.

🔹 Lesson 2: Language Usability: Modern Syntax and Templates

Overview: This lesson explores the evolution of C++ from C++11 through C++20, focusing on enhancements that improve code clarity, type safety, and template flexibility. Students will master modern syntax features like type inference, compile-time evaluation with constexpr, and advanced template techniques including variadic templates and fold expressions.

Learning Outcomes:

  • Resolve pointer ambiguity using nullptr and implement compile-time logic using constexpr and if constexpr.
  • Simplify complex declarations using auto, decltype, and type aliasing (using) while ensuring robust object-oriented design with override and final.
  • Design highly generic code using variadic templates, fold expressions, and non-type template parameter deduction.

🔹 Lesson 3: Runtime Improvements: Lambdas and Move Semantics

Overview: This lesson covers critical runtime enhancements introduced in modern C++ (C++11/14), focusing on functional programming constructs and efficient resource management. Students will master Lambda expressions—including C++14 generic and expression captures—and delve into the mechanics of Move Semantics, Value Categories (Lvalues/Rvalues), and Perfect Forwarding to eliminate unnecessary deep copies in high-performance applications.

Learning Outcomes:

  • Define and implement Lambda expressions using various capture modes (Value, Reference, Expression, and Generic).
  • Utilize std::function and std::bind to create flexible function wrappers and partial function applications.
  • Categorize expressions into Lvalues, Prvalues, and Xvalues, and apply Move Semantics to optimize class resource management.

🔹 Lesson 4: Modern Containers: std::array

Overview: This lesson introduces std::array, a container introduced in C++11 as a modern alternative to traditional C-style arrays. It explores the benefits of using a fixed-size container that encapsulates raw arrays while providing a safer, STL-compatible interface and stack-based memory allocation.

Learning Outcomes:

  • Differentiate between the memory models and use cases of std::array (fixed size, stack) and std::vector (dynamic size, heap).
  • Correctly initialize and manage std::array using member functions like .size() and .empty().
  • Apply Standard Template Library (STL) algorithms and range-based loops to std::array objects.

🔹 Lesson 5: Smart Pointers and RAII Memory Management

Overview: This lesson introduces Resource Acquisition Is Initialization (RAII) as the foundational philosophy of modern C++ memory management. It covers the transition from manual pointer handling to automated management using std::unique_ptr for exclusive ownership, std::shared_ptr for reference-counted sharing, and std::weak_ptr for breaking circular dependencies.

Learning Outcomes:

  • Understand and apply the RAII principle to ensure resources are released when they go out of scope.
  • Implement shared and exclusive ownership models using std::shared_ptr and std::unique_ptr.
  • Identify and resolve memory leaks caused by circular references using std::weak_ptr.

🔹 Lesson 6: Regular Expressions and String Parsing

Overview: This lesson covers the implementation of Regular Expressions using the C++11 standard library (std::regex) and their practical application in parsing HTTP requests. Students will learn the syntax of regex quantifiers, the use of std::smatch for result capturing, and the architectural integration of these tools within a template-based Web Server environment.

Learning Outcomes:

  • Identify and apply regular expression special characters (quantifiers) to define string patterns.
  • Utilize std::regex and std::smatch to validate filenames and extract data from strings.
  • Implement core server logic, including HTTP request parsing and resource mapping using modern C++ memory models and containers.

🔹 Lesson 7: Parallelism, Concurrency, and Memory Models

Overview: This lesson covers the fundamental shift in C++11 toward a standardized multi-threading model. It transitions from basic thread management and RAII-based locking to advanced synchronization primitives like futures and condition variables, concluding with the rigorous C++11 memory model, atomic operations, and memory consistency guarantees.

Learning Outcomes:

  • Manage thread lifecycles and protect shared resources using std::thread, std::mutex, and RAII wrappers (std::lock_guard, std::unique_lock).
  • Implement asynchronous workflows and thread communication using std::future, std::packaged_task, and std::condition_variable.
  • Apply std::atomic types and select appropriate std::memory_order strategies to ensure memory visibility and prevent illegal instruction reordering.

🔹 Lesson 8: Standard Library: Filesystem Updates

Overview: This lesson explores the evolution of the C++ Standard Library, focusing on the high-level features introduced in C++11 and subsequent standards. It highlights the transition from platform-specific or low-level implementations to standardized, high-level utilities like std::filesystem and concurrency tools.

Learning Outcomes:

  • Identify the key concurrency components added to the Standard Library in C++11.
  • Recognize std::filesystem as a major addition for modern path and file management.
  • Understand the role of high-level abstractions in modernizing C++ development.

🔹 Lesson 9: Miscellaneous Modern Features

Overview: This lesson covers a selection of impactful features introduced in C++11 and refined in later standards to improve type safety, performance, and code readability. Students will learn about the long long int type, exception specifications with noexcept, enhanced string and user-defined literals, and fine-grained control over memory alignment via alignof and alignas.

Learning Outcomes:

  • Identify and implement the long long int type for extended integer precision.
  • Apply the noexcept specifier to optimize exception handling and use the noexcept operator to query function safety.
  • Utilize Raw String Literals to simplify complex string definitions and create User-defined Literals for custom type suffixes.

🔹 Lesson 10: C++20: The Future of the Language

Overview: This lesson introduces the "Big Four" pillars of C++20: Concepts, Modules, Coroutines, and Ranges. These features represent the most significant evolution of the language since C++11, focusing on improving template safety, compilation efficiency, asynchronous programming, and functional-style data processing.

Learning Outcomes:

  • Identify and describe the four major features introduced in the C++20 standard.
  • Explain the role of Concepts and Constraints in improving template error handling and design.
  • Differentiate between the traditional header-based approach and the new Modules system.