現代 C++ 教學
本教程旨在為有經驗的開發者提供 C++11/14/17/20 新功能的快速參考。內容涵蓋語言易用性提升、執行時間改進、新容器、智慧指針與記憶體管理、正則表示式、並行程式設計,以及 C++20 的預覽。
課程總覽
📚 內容摘要
本教程旨在為有經驗的開發者提供一份快速參考,介紹 C++11/14/17/20 的新功能。內容涵蓋語言易用性提升、執行時期優化、新容器的使用、智慧指標與記憶體管理、正規表示式、並行程式設計,以及 C++20 的預覽。
快速掌握 C++11/14/17/20,邁入現代 C++ 程式設計的大門。
作者: 欧长坤 (Ou Changkun)
致謝: 采用 CC BY-NC-ND 4.0 協議授權,感謝所有 GitHub 貢獻者及相關社群支持。
🎯 學習目標
- 使用命令列工具識別並驗證 C++ 編譯器環境與目標架構。
- 解釋 C(C89、C99、C11)與 C++(C++98、C++1x)之間的標準相容性與重疊歷史。
- 透過
extern "C"標頭與多階段編譯工作流程,實現跨語言連結功能。 - 使用
nullptr消除指標歧義,並利用constexpr與if constexpr實作編譯時期邏輯。 - 使用
auto、decltype及型別別名(using)簡化複雜宣告,同時以override與final確保穩固的物件導向設計。 - 利用可變參數模板、折疊表達式與非類型模板參數推導,設計高度通用的程式碼。
- 使用多種捕捉模式(值、引用、運算式、通用)定義並實作 Lambda 表達式。
- 使用
std::function與std::bind建立彈性函數包裝器與部分函數應用。 - 將表達式分類為左值(Lvalues)、純右值(Prvalues)與臨時量(Xvalues),並應用搬移語意(Move Semantics)優化類別資源管理。
- 区分
std::array(固定大小、棧上)與std::vector(動態大小、堆上)的記憶體模型與使用情境。
課程 共 10 课时 · 预计 30.0h
概要: 本課介紹 C++20 的「四大支柱」:概念(Concepts)、模組(Modules)、協程(Coroutines)與範圍(Ranges)。這些功能代表自 C++11 以來語言最重大的演進,著重於提升模板安全性、編譯效率、非同步程式設計與函數式風格的資料處理。
學習成果:
- 認識並描述 C++20 標準引入的四項主要功能。
- 解釋概念(Concepts)與限制(Constraints)在改善模板錯誤處理與設計中的角色。
- 辨別傳統基於標頭檔的方式與新的模組系統之間的差異。
課程
Lesson
This lesson explores the evolution of C++ and the importance of adopting modern, type-safe coding practices over legacy C-style habits. Students will learn to utilize explicit casting operators and compiler diagnostics to improve code stability, security, and performance.
This lesson explores modern C++ language usability by addressing the "NULL Trap" through the introduction of `nullptr` and examining how `constexpr` and `noexcept` shift computation and error handling to compile-time. Students will learn to improve code safety and performance by replacing legacy C-style macros and casts with modern, type-safe alternatives.
This lesson explores performance optimization in modern C++ through move semantics, lambda expressions, and efficient memory management. Students will learn to leverage zero-cost abstractions like std::array and SFINAE to improve code performance and type safety in resource-constrained environments.
This lesson explores the transition from legacy C-style arrays to the modern std::array, highlighting how the latter provides type safety and STL integration without sacrificing performance. Students will learn to leverage stack-based memory allocation, bounds-checking, and perfect forwarding to write more efficient and robust C++ code.
This lesson explores the pitfalls of manual memory management, such as memory leaks and dangling pointers, and introduces RAII (Resource Acquisition Is Initialization) as a safer, automated alternative. Students will learn how to utilize smart pointers like std::unique_ptr, std::shared_ptr, and std::weak_ptr to ensure deterministic resource cleanup and exception safety in modern C++.
This lesson explores the transition from imperative, manual string searching to declarative pattern matching using regular expressions and formal grammars. Students will learn to apply regex metacharacters and anchors to efficiently parse unstructured data while understanding the performance benefits of using Deterministic Finite Automata.
This lesson explores the transition to standardized concurrency in C++11, which introduced a formal memory model and native thread support to replace platform-specific APIs. Students will learn to manage thread lifecycles, ensure cross-platform data consistency with standardized types, and use the noexcept specifier to maintain program stability in multi-threaded environments.
This lesson explores the evolution of C++ from platform-specific coding to a unified, high-level abstraction layer, focusing on the transition from legacy macros to modern standards. Students will learn to leverage C++11 concurrency features and C++23 filesystem utilities to write portable, maintainable code that avoids OS-specific dependencies.
This lesson explores modernizing C++ development by transitioning from platform-specific APIs to standardized, high-level abstractions like task-based parallelism and the C++11 memory model. Students will learn to improve code maintainability and safety by utilizing features such as std::future, std::atomic, and extended integer types like long long int.
This lesson explores the evolution of C++20, focusing on the "Big Four" features—Concepts, Modules, Coroutines, and Ranges—that shift the language toward a more declarative and efficient programming model. Students will learn how these tools improve code safety, reduce build times, and replace complex template metaprogramming with clearer, constrained syntax.