本书是一本对基本概念和语言特性有着深刻见解的指南性书籍,同时也是一本包含内容广泛的参考性书籍。本书首先讲述了语言细节,然后对编码技巧进行了广泛的讨论,还对模板的高级应用进行了阐述、分析。贯串全书的示例阐明了抽象概念并演示了实践方法。 读者将学习到: ·正确的模板行为; ·如何避开与模板相关的错误陷阱; ·从基础的到早前没有文字约定的惯用语法和技巧; ·如何在不对性能和安全性造成损害的前提下进行源代码重用; ·如何提高C++程序的效率; ·如何构建更具灵活性和可维护性的软件。
模板是C++功能强大的特性之一,然而却常常被人们所忽视、误解与误用。本书将帮助软件设计师、工程师清晰理解为什么、何时及如何使用模板来构建及维护简洁、快速、灵活的高效软件。本书将引导程序员完整地开发C++功能强大的特性——模板特性。
David Vandevoorde是20多年的C++工作经验,C++标准委员会成员。曾因为审校《The C++ Programming Language》一书的草稿,获得Bjarne Stroustrup的推荐,编写了《C++ Solutions》。
Preface
Acknowledgments
1 About This Book
1.1 What You Should Know Before Reading This Book
1.2 Overall Structure of the Book
1.3 How to Read This Book
1.4 Some Remarks About Programming Style
1.5 The Standard versus Reality
1.6 Example Code and Additional Informations
1.7 Feedback
Part I:The Basics
2 Function Templates
2.1 A First Look at Function Templates
2.1.1 Defining the Template
2.1.2 Using the Template
2.2 Argument Deduction
2.3 Template Parameters
2.4 Overloading Function Templates
2.5 Summary
3 Class Templates
3.1 Implementation of Class Template Stack
3.1.1 Declaration of Class templates
3.1.2 Implementation of Member Functions
3.2 Use of Class Template Stack
3.3 Specializations of Class Templates
3.4 Partial Specialization
3.5 Default Template Arguments
3.6 Summary
4 Nontype Template Parameters
4.1 Nontype Class Template Parameters
4.2 Nontype Function Template Parameters
4.3 Restrictions for Nontype Template Parameters
4.4 Summary
5 Tricky Basics
5.1 Keyword typename
5.2 Using this->
5.3 Member Templates
5.4 Template Template Parameters
5.5 Zero Initialization
5.6 Using String Literals as Arguments for Function Templates
5.7 Summary
6 Using Templates in Practice
6.1 The Inclusion Model
6.1.1 Linker Errors
6.1.2 Templates in Header Files
6.2 Explicit Instantiation
6.2.1 Example of Explicit Instantiation
6.2.2 Combining the Inclusion Model and Explicit Instantiation
6.3 The Separation Model
6.3.1 The Keyword export
6.3.2 Limitations of the Separation Model
6.3.3 Preparing for the Separation Model
6.4 Templates and inline
6.5 Precompiled Headers
6.6 Debugging Templates
6.6.1 Decoding the Error Novel
6.6.2 Shallow Instantiation
6.6.3 Long Symbols
6.6.4 Tracers
6.6.5 Oracles
6.6.6 Archetypes
6.7 Afternotes
6.8 Summary
7 Basic Template Terminology
7.1 "Class Template"or"Template Class"?
7.2 Instantiation and Specialization
7.3 Declarations versus Definitions
7.4 The One-Definition Rule
7.5 Template Arguments versus Template Parameters
Part II:Templates in Depth
8 Fundamentals in Depth
8.1 Parameterized Declarations
8.1.1 Virtual Member Functions
8.1.2 Linkage of Templates
8.1.3 Primary Templates
8.2 Template Parameters
8.2.1 Type Parameters
8.2.2 Nontype Parameters
8.2.3 Template Template Parameters
8.2.4 Default Template Arguments
8.3 Template Arguments
8.3.1 Function Template Arguments
8.3.2 Type Arguments
8.3.3 Nontype Arguments
8.3.4 Template Template Arguments
8.3.5 Equivalence
8.4 Friends
8.4.1 Friend Functions
8.4.2 Friend Templates
8.5 Afternotes
9 Names in Templates
10 Instantiation
11 Template Argument Deduction
12 Specialization and Overloading
13 Future Directions
Part III:Templates and Design
14 The Polymorphic Power of Templates
15 Traits and Policy Classes
16 Templates and Inheritance
17 Metaprograms
18 Expression Templates
19 Type Classification
20 Smart Pointers
21 Tuples
22 Function Objects and Callbacks
Appendixes
A The One-Definition Rule
B Overload Resoution
Bibliography
Glossary
Index