没有合适的资源?快使用搜索试试~ 我知道了~
首页more_c++_idioms.pdf
more_c++_idioms.pdf

more_c++_idioms.pdf more_c++_idioms.pdf more_c++_idioms.pdf
资源详情
资源评论
资源推荐

More C++ Idioms/Print Version
1
More C+ + Idioms/ Print Version
Preface
C++ has indeed become too "expert friendly" --- Bjarne Stroustrup, The Problem with Programming
[1]
,
Technology Review, Nov 2006.
Stroustrup's saying is true because experts are intimately familiar with the idioms in the language. With the increase in
the idioms a programmer understands, the language becomes friendlier to him or her. The objective of this open
content book is to present modern C++ idioms to programmers who have moderate level of familiarity with C++, and
help elevate their knowledge so that C++ feels much friendlier to them. It is designed to be an exhaustive catalog of
reusable idioms that expert C++ programmers often use while programming or designing using C++. This is an effort
to capture their techniques and vocabulary into a single work. This book describes the idioms in a regular format:
Name-Intent-Motivation-Solution-References, which is succinct and helps speed learning. By their nature, idioms tend
to have appeared in the C++ community and in published work many times. An effort has been made to refer to the
original source(s) where possible; if you find a reference incomplete or incorrect, please feel free to suggest or make
improvements.
The world is invited to catalog reusable pieces of C++ knowledge (similar to the book on design patterns by GoF).
The goal here is to first build an exhaustive catalog of modern C++ idioms and later evolve it into an idiom language,
just like a pattern language. Finally, the contents of this book can be redistributed under the terms of the GNU Free
Documentation License.
Aimed toward: Anyone with an intermediate level of knowledge in C++ and supported language paradigms
Authors
€ Sumant Tambe•
talk
-- The initiator and lead contributor since July 2007.
€ Many other C++ aficionados who continuously improve the writeup, examples, and references where necessary.
Praise of the Book
€ "Great and valuable work!" -- Bjarne Stroustrup (February, 2009)
Table of Contents
Note: synonyms for each idiom are listed in parentheses.
1. Adapter Template
2. Address Of
3. Algebraic Hierarchy
4. Attach by Initialization
5. Barton-Nackman trick
6. Base-from-Member
7. Boost mutant
8. Calling Virtuals During Initialization
9. Capability Query
10. Checked delete
11. Clear-and-minimize
12. Coercion by Member Template
13. Compile Time Control Structures
14. Computational Constructor
15. Concrete Data Type

More C++ Idioms/Print Version
2
16. Const auto_ptr
17. Construct On First Use
18. Construction Tracker
19. Copy-and-swap
20. Copy-on-write
21. Counted Body (intrusive reference counting)
22. Curiously Recurring Template Pattern
23. Detached Counted Body (non-intrusive reference counting)
24. Empty Base Optimization
25. Emulated Exception
26. enable-if
27. Envelope Letter
28. Erase-Remove
29. Examplar
30. Execute-Around Pointer
31. Export Guard Macro
32. Expression-template
33. Fake Vtable
34. Fast Pimpl
35. Final Class
36. Free Function Allocators
37. Friendship and the Attorney-Client
38. Function Object
39. Generic Container Idioms
40. Include Guard Macro
41. Inline Guard Macro
42. Inner Class
43. Int-To-Type
44. Interface Class
45. Iterator Pair
46. Making New Friends
47. Metafunction
48. Move Constructor
49. Multi-statement Macro
50. Multiple Member Initialization
51. Member Detector
52. Named Constructor
53. Named External Argument
54. Named Loop (labeled loop)
55. Named Parameter
56. Named Template Parameters
57. Nifty Counter (Schwarz Counter)
58. Non-copyable Mixin
59. Non-member get
60. Non-member Non-friend Function
61. Non-throwing swap
62. Non-Virtual Interface (Public Overloaded Non-Virtuals Call Protected Non-Overloaded Virtuals)

More C++ Idioms/Print Version
3
63. nullptr
64. Object Generator
65. Object Template
66. Overload Set Creation
67. Parameterized Base Class (Parameterized Inheritance)
68. Pimpl (Handle Body, Compilation Firewall, Cheshire Cat)
69. Policy Clone (Metafunction wrapper)
70. Policy-based Design
71. Polymorphic Exception
72. Recursive Type Composition
73. Resource Acquisition Is Initialization (RAII, Execute-Around Object, Scoped Locking)
74. Resource Return
75. Return Type Resolver
76. Runtime Static Initialization Order Idioms
77. Safe bool
78. Scope Guard
79. Substitution Failure Is Not An Error (SFINAE)
80. Shortening Long Template Names
81. Shrink-to-fit
82. Small Object Optimization
83. Smart Pointer
84. Storage Class Tracker
85. Tag Dispatching
86. Temporary Base Class
87. The result_of technique
88. Thin Template
89. Traits
90. Type Erasure
91. Type Generator (Templated Typedef)
92. Type Safe Enum
93. Type Selection
94. Virtual Constructor
95. Virtual Friend Function
Algebraic Hierarchy
Intent
To hide multiple closely related algebraic abstractions (numbers) behind a single generic abstraction and provide a
generic interface to it.
Also Known As
€ State (Gamma et al.)
Motivation
In pure object-oriented languages like Smalltalk, variables are run-time bindings to objects that act like labels.
Binding a variable to an object is like sticking a label on it. Assignment in these languages is analogous to peeling a

More C++ Idioms/Print Version
4
label off one object and putting it on another. On the other hand, in C and C++, variables are synonyms for addresses
or offsets instead of being labels for objects. Assignment does not mean re-labelling, it means overwriting old
contents with new one. Algebraic Hierarchy idiom uses delegated polymorphism to simulate weak variable to object
binding in C++. Algebraic Hierarchy uses Envelope Letter idiom in its implementation. The motivation behind this
idiom is to be able write code like the one below.
Number n1 = Complex (1, 2); // Label n1 for a complex number
Number n2 = Real (10); // Label n2 for a real number
Number n3 = n1 + n2; // Result of addition is labelled n3
Number n2 = n3; // Re-labelling
Solution and Sample Code
Complete code showing implementation of Algebraic Hierarchy idiom is shown below.
#include <iostream>
using namespace std;
struct BaseConstructor { BaseConstructor(int=0) {} };
class RealNumber;
class Complex;
class Number;
class Number
{
friend class RealNumber;
friend class Complex;
public:
Number ();
Number & operator = (const Number &n);
Number (const Number &n);
virtual ~Number();
virtual Number operator + (Number const &n) const;
void swap (Number &n) throw ();
static Number makeReal (double r);
static Number makeComplex (double rpart, double ipart);
protected:
Number (BaseConstructor);
private:
void redefine (Number *n);
virtual Number complexAdd (Number const &n) const;
virtual Number realAdd (Number const &n) const;

More C++ Idioms/Print Version
5
Number *rep;
short referenceCount;
};
class Complex : public Number
{
friend class RealNumber;
friend class Number;
Complex (double d, double e);
Complex (const Complex &c);
virtual ~Complex ();
virtual Number operator + (Number const &n) const;
virtual Number realAdd (Number const &n) const;
virtual Number complexAdd (Number const &n) const;
double rpart, ipart;
};
class RealNumber : public Number
{
friend class Complex;
friend class Number;
RealNumber (double r);
RealNumber (const RealNumber &r);
virtual ~RealNumber ();
virtual Number operator + (Number const &n) const;
virtual Number realAdd (Number const &n) const;
virtual Number complexAdd (Number const &n) const;
double val;
};
/// Used only by the letters.
Number::Number (BaseConstructor)
: rep (0),
referenceCount (1)
{}
/// Used by user and static factory functions.
Number::Number ()
: rep (0),
referenceCount (0)
剩余136页未读,继续阅读
安全验证
文档复制为VIP权益,开通VIP直接复制

评论1