C++ Templates (Chinese Edition) cover

C++ Templates (Chinese Edition)

Rating 4.3
Chapters
24
Series
C and C++ in Practice Series
ISBN
9787115171818

Tags

Summary

This book is a systematic guide to C++ template programming, using three threads—"basic concepts, common techniques, and practical examples"—to help readers build a solid foundation in templates. It has 22 chapters in four parts. Part One introduces template fundamentals in tutorial style: writing function and class templates, template argument deduction, template parameters, default template arguments, and overloading. Part Two delves into the language details of templates, discussing rules that are easily overlooked, such as name lookup, instantiation, specialization and partial specialization, inheritance in templates, and dependent names. Part Three introduces the basic design techniques templates enable, such as traits, policy classes, and the CRTP. Part Four applies these techniques to real scenarios, covering metaprogramming and expression templates. Appendices A and B compile material on the One Definition Rule and overload resolution, respectively, for handy desk reference. The authors, David Vandevoorde and Nicolai M. Josuttis, are both members of the C++ standards committee; the extensively commented code examples explain not only "how to write" but also "why to write it this way" and how the compiler's two-phase lookup works. The content is based on C++98/03, so it serves as a foundational reference for understanding the template compilation model; readers should note how it maps to features added in later standards.

Editor's review

C++ 模板的难点不在于写出一个 template 关键字,而在于理解编译器如何查找名字、如何实例化、如何推导实参,以及为什么某些看起来合法的代码会突然报出难以阅读的错误。这本书正是围绕这些“看不见的规则”展开:它把模板当作一门语言来讲解,从语法一路走到元编程与表达式模板,并把重载解析、一处定义原则这类日常最容易踩坑的主题放进附录,方便随时翻查。两位作者都是 C++ 标准委员会成员,示例代码配有大量注释,解释的是编译器真实行为,而非简化后的教学模型。第 1 版在中文读者中长期保持高分评价,历经多次重印,是国内 C++ 进阶书单的常客。需要提醒的是,本书内容以 C++98/03 为准,变参模板、泛型 lambda、类模板参数推导等现代特性并未覆盖;读它可以打牢底层认知,再读第 2 版(何荣华等译,2022 年后出版)则能补齐现代 C++ 的拼图。两者配合,是模板学习最稳妥的路径。

Who it's for

A good fit for

  • 已掌握 C++ 基本语法,想系统理解模板机制的开发者
  • 需要阅读或维护 STL、Boost 等模板库源码的工程师
  • 准备编写泛型库、希望减少编译期错误的 C++ 程序员
  • 对模板元编程、表达式模板等编译期技术感兴趣的学习者

Not a good fit for

  • 刚接触编程或尚未熟练使用 C++ 类的零基础读者
  • 只想了解 C++11 之后新特性、不关心底层机制的读者
  • 主要使用 Java、Python 等不支持模板的语言的开发者

Reading tips

  1. 先按第 1 部分顺序通读函数模板与类模板两章,并动手复现示例代码。
  2. 第 2 部分语言细节可当参考手册,遇到实际编译错误时按主题查阅。
  3. 阅读第 3、4 部分前先熟悉 STL 容器与迭代器,否则 trait 与策略类难以体会。
  4. 读完后对照第 2 版补读变参模板、类模板参数推导等现代特性章节。

About the publisher

Posts & Telecom Press China · Founded in 1953

Posts & Telecom Press is known for its strengths in science and technology, computing, and information and communications titles, and has extended its publishing into photography, art, software learning, and inspirational growth, giving it a fairly diverse range. Its catalog includes computing and communications works such as Refactoring and Patterns (Revised Edition) and Huawei Switch Learning Guide (Second Edition), alongside bilingual collector's editions such as The Little Prince (Chinese-English-French illustrated collector's edition), The Great Gatsby, and Three Days to See. Beijing Ten Years: 1980-1990 and Ghost Blade: WLOP Personal Illustration Collection reflect its presence in photography and art. Its authors include Antoine de Saint-Exupéry, Helen Keller, Joshua Kerievsky, Wang Da, Zhang Zhaozeng, and WLOP.

Contents

  1. Chapter 1: About This Book
  2. Part 1: Template Fundamentals: Chapter 2: Function Templates
  3. Chapter 3: Class Templates
  4. Chapter 4: Non-Type Template Parameters
  5. Chapter 5: Tricky Fundamentals
  6. Chapter 6: Templates in Practice
  7. Chapter 7: Template Terminology
  8. Part 2: Templates in Depth: Chapter 8: Template Fundamentals in Depth
  9. Chapter 9: Names in Templates
  10. Chapter 10: Instantiation
  11. Chapter 11: Template Argument Deduction
  12. Chapter 12: Specialization and Overloading
  13. Chapter 13: Future Directions
  14. Part 3: Templates and Design: Chapter 14: Polymorphism in Templates
  15. Chapter 15: Traits and Policy Classes
  16. Chapter 16: Templates and Inheritance
  17. Chapter 17: Metaprogramming
  18. Chapter 18: Expression Templates
  19. Part 4: Advanced Applications: Chapter 19: Type Classification
  20. Chapter 20: Smart Pointers
  21. Chapter 21: Tuples
  22. Chapter 22: Function Objects and Functors
  23. Appendix A: The One Definition Rule
  24. Appendix B: Overload Resolution

FAQ

这本书和《C++ Templates(第2版)中文版》有什么区别?
本书基于 C++98/03,共 22 章,由陈伟柱翻译;第 2 版加入了 Douglas Gregor,内容按 C++11/14/17 重写,由何荣华等翻译,删去了过时实现。初学模板底层机制可先读本书,再读第 2 版掌握现代特性。
零基础可以直接读这本书吗?
不建议。书中默认读者熟悉类、继承、运算符重载与编译链接流程,缺少这些基础会使前几章的示例难以理解。建议先读一本 C++ 入门书并写过若干含模板的代码再来。
书中的示例代码能直接编译运行吗?
多数示例针对 C++98/03,用现代编译器(如 GCC、Clang 的 C++17 模式)编译可能出现告警或差异。遇到问题时切换到 -std=c++03 编译选项,并注意书中讨论的部分缺陷在后续标准中已被修正。
读这本书之前需要先读《C++标准库》吗?
不是必须,但会很有帮助。书中用 std::vector、迭代器、函数对象等讨论 trait 与策略类,若熟悉标准库的接口设计,理解设计技术部分会顺畅很多;Josuttis 的《C++标准库》可作为配套读物。

Comments

Find this book

This site is an index: you start “Search all sources” in your browser, and the entries it lists all point to third-party sites; please judge their availability yourself.

More by the same author

Top rated in the same category