More Exceptional C++: 40
New Engineering Puzzles,
Programming Problems, and
Solutions
More Exceptional C++: 40 New Engineering Puzzles, Programming Problems, and Solutions
1
Table of Contents
1. Main Page ................................................................................................................................. 5
2. Table of content ........................................................................................................................ 5
3. Copyright ................................................................................................................................. 11
4. Foreword ................................................................................................................................. 13
5. Preface .................................................................................................................................... 14
5.1 What's 'More?' .................................................................................................................. 15
5.2 What I Assume You Know ................................................................................................. 15
5.3 How to Read This Book ..................................................................................................... 15
5.4 Namespaces, Typename, References, and Other Conventions ........................................ 16
5.5 Acknowledgments............................................................................................................. 17
6. Generic Programming and the C++ Standard Library ............................................................. 18
6.1 Item 1. Switching Streams ................................................................................................ 18
6.1.1 Solution ...................................................................................................................... 19
6.2 Item 2. Predicates, Part 1: What 'remove()' Removes ...................................................... 22
6.2.1 Solution ...................................................................................................................... 23
6.3 Item 3. Predicates, Part 2: Matters of State ..................................................................... 27
6.3.1 Solution ...................................................................................................................... 27
6.4 Item 4. Extensible Templates: Via Inheritance or Traits? ................................................. 34
6.4.1 Solution ...................................................................................................................... 35
6.5 Item 5. Typename ............................................................................................................. 47
6.5.1 Solution ...................................................................................................................... 47
6.6 Item 6. Containers, Pointers, and Containers That Aren't ................................................ 51
6.6.1 Solution ...................................................................................................................... 51
6.7 Item 7. Using 'vector' and 'deque' .................................................................................... 59
6.7.1 Solution ...................................................................................................................... 60
6.8 Item 8. Using 'set' and 'map' ............................................................................................. 66
6.8.1 Solution ...................................................................................................................... 67
6.9 Item 9. Equivalent Code? .................................................................................................. 72
6.9.1 Solution ...................................................................................................................... 72
6.10 Item 10. Template Specialization and Overloading ........................................................ 76
6.10.1 Solution .................................................................................................................... 78
6.11 Item 11. Mastermind ...................................................................................................... 82
More Exceptional C++: 40 New Engineering Puzzles, Programming Problems, and Solutions
2
6.11.1 Simplified Rules Summary ....................................................................................... 82
6.11.2 Solution .................................................................................................................... 83
7. Optimization and Performance............................................................................................... 93
7.1 Item 12. 'inline' ................................................................................................................. 94
7.1.1 Solution ...................................................................................................................... 94
7.2 Item 13. Lazy Optimization, Part 1: A Plain Old String ...................................................... 97
7.2.1 Solution ...................................................................................................................... 98
7.3 Item 14. Lazy Optimization, Part 2: Introducing Laziness ............................................... 100
7.3.1 Solution .................................................................................................................... 102
7.4 Item 15. Lazy Optimization, Part 3: Iterators and References ........................................ 104
7.4.1 Solution .................................................................................................................... 105
7.5 Item 16. Lazy Optimization, Part 4: Multithreaded Environments ................................. 112
7.5.1 Solution .................................................................................................................... 113
8. Exception Safety Issues and Techniques ............................................................................... 122
8.1 Item 17. Constructor Failures, Part 1: Object Lifetimes ................................................. 123
8.1.1 Solution .................................................................................................................... 123
8.2 Item 18. Constructor Failures, Part 2: Absorption? ........................................................ 126
8.2.1 Solution .................................................................................................................... 126
8.3 Item 19. Uncaught Exceptions ........................................................................................ 133
8.3.1 Solution .................................................................................................................... 133
8.4 Item 20. An Unmanaged Pointer Problem, Part 1: Parameter Evaluation ..................... 138
8.4.1 Solution .................................................................................................................... 139
8.5 Item 21. An Unmanaged Pointer Problem, Part 2: What About 'auto_ptr?' .................. 141
8.5.1 Solution .................................................................................................................... 142
8.6 Item 22. Exception-Safe Class Design, Part 1: Copy Assignment .................................... 147
8.6.1 Solution .................................................................................................................... 147
8.7 Item 23. Exception-Safe Class Design, Part 2: Inheritance ............................................. 154
8.7.1 Solution .................................................................................................................... 155
9. Inheritance and Polymorphism ............................................................................................. 158
9.1 Item 24. Why Multiple Inheritance? ............................................................................... 159
9.1.1 Solution .................................................................................................................... 159
9.2 Item 25. Emulating Multiple Inheritance ........................................................................ 162
9.2.1 Solution .................................................................................................................... 164
9.3 Item 26. Multiple Inheritance and the Siamese Twin Problem ...................................... 166
More Exceptional C++: 40 New Engineering Puzzles, Programming Problems, and Solutions
3
9.3.1 Solution .................................................................................................................... 166
9.4 Item 27. (Im)pure Virtual Functions ................................................................................ 170
9.4.1 Solution .................................................................................................................... 170
9.5 Item 28. Controlled Polymorphism ................................................................................. 175
9.5.1 Solution .................................................................................................................... 176
10. Memory and Resource Management ................................................................................. 177
10.1 Item 29. Using 'auto_ptr' .............................................................................................. 177
10.1.1 Solution .................................................................................................................. 178
10.2 Item 30. Smart Pointer Members, Part 1: A Problem with 'auto_ptr' .......................... 184
10.2.1 Solution .................................................................................................................. 185
10.3 Item 31. Smart Pointer Members, Part 2: Toward a 'ValuePtr' .................................... 189
10.3.1 Solution .................................................................................................................. 189
11. Free Functions and Macros ................................................................................................. 199
11.1 Item 32. Recursive Declarations ................................................................................... 200
11.1.1 Solution .................................................................................................................. 200
11.2 Item 33. Simulating Nested Functions .......................................................................... 204
11.2.1 Solution .................................................................................................................. 205
11.3 Item 34. Preprocessor Macros ...................................................................................... 212
11.3.1 Solution .................................................................................................................. 213
11.4 Item 35. #Definition ...................................................................................................... 215
11.4.1 Solution .................................................................................................................. 215
12. Miscellaneous Topics .......................................................................................................... 220
12.1 Item 36. Initialization .................................................................................................... 220
12.1.1 Solution .................................................................................................................. 221
12.2 Item 37. Forward Declarations ..................................................................................... 223
12.2.1 Solution .................................................................................................................. 223
12.3 Item 38. 'Typedef' ......................................................................................................... 225
12.3.1 Solution .................................................................................................................. 225
12.4 Item 39. Namespaces, Part 1: Using-Declarations and Using-Directives...................... 228
12.4.1 Solution .................................................................................................................. 228
12.5 Item 40. Namespaces, Part 2: Migrating to Namespaces ............................................. 230
12.5.1 Solution .................................................................................................................. 231
12.6 Afterword ...................................................................................................................... 239
13. Appendix A. Optimizations That Aren't (in a Multithreaded World) .................................. 240
More Exceptional C++: 40 New Engineering Puzzles, Programming Problems, and Solutions
4
13.1 Introduction .................................................................................................................. 240
13.2 What's the Fuss About? ................................................................................................ 241
13.3 Recap: Plain Old 'Original:: String' (Item 13) ................................................................. 242
13.4 Using a Plain Old String: Single-Threaded ..................................................................... 242
13.5 Using a Plain Old String: Multithreaded ....................................................................... 243
13.6 Now Throw in an 'Optimization': Copy-On-Write (COW) ............................................. 246
13.7 Using a COW String: Multithreaded ............................................................................. 246
13.8 Mild versus Severe Inefficiencies .................................................................................. 248
13.9 Some Actual Numbers .................................................................................................. 250
13.10 A Real-World Example: C++'s 'basic_string' ................................................................ 252
13.11 'Am I Affected?' ........................................................................................................... 253
13.12 Summary ..................................................................................................................... 254
14. Appendix B. Test Results for Single-Threaded versus Multithread-Safe String
Implementations ....................................................................................................................... 254
14.1 Approach ....................................................................................................................... 254
14.2 Raw Measurements ...................................................................................................... 256
14.3 Test Harness .................................................................................................................. 260
15. Bibliography ........................................................................................................................ 260