本書是一本對基本概念和語言特性有著深刻見解的指南性書籍,同時也是一本包含內(nèi)容廣泛的參考性書籍。本書首先講述了語言細(xì)節(jié),然后對編碼技巧進(jìn)行了廣泛的討論,還對模板的高級應(yīng)用進(jìn)行了闡述、分析。貫串全書的示例闡明了抽象概念并演示了實(shí)踐方法。 讀者將學(xué)習(xí)到: ·正確的模板行為; ·如何避開與模板相關(guān)的錯誤陷阱; ·從基礎(chǔ)的到早前沒有文字約定的慣用語法和技巧; ·如何在不對性能和安全性造成損害的前提下進(jìn)行源代碼重用; ·如何提高C++程序的效率; ·如何構(gòu)建更具靈活性和可維護(hù)性的軟件。
模板是C++功能強(qiáng)大的特性之一,然而卻常常被人們所忽視、誤解與誤用。本書將幫助軟件設(shè)計師、工程師清晰理解為什么、何時及如何使用模板來構(gòu)建及維護(hù)簡潔、快速、靈活的高效軟件。本書將引導(dǎo)程序員完整地開發(fā)C++功能強(qiáng)大的特性——模板特性。
David Vandevoorde是20多年的C++工作經(jīng)驗,C++標(biāo)準(zhǔn)委員會成員。曾因為審校《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