I l@ve RuBoard
Table of Contents
•
More Exceptional C++
By Herb Sutter
Publisher
Pub Date
ISBN
Pages
: Addison Wesley
: December 17, 2001
: 0-201-70434-X
: 304
Organized in a practical problem-and-solution format, More Exceptional C++ picks up where the
widely acclaimed Exceptional C++ leaves off, providing successful strategies for solving real-world
problems in C++. Drawing from years of in-the-trenches experience, Herb Sutter provides tested
techniques and practical solutions for programmers designing modern software systems with C++,
from small projects to enterprise applications.
Built around forty programming puzzles, More Exceptional C++ helps you understand the rules and
issues critical to successful software design and development in C++. New themes included in this
sequel place a strong emphasis on generic programming, memory management, and using the C++
standard library, including coverage of important techniques like traits and predicates. Also included
are guidelines and considerations to remember when using standard containers and algorithms--topics
rarely covered in-depth in other sources.
Readers will find solutions to such important questions as:
What pitfalls might you encounter when using std::map and std::set, and how can you safely
avoid them?
What kinds of predicates are safe to use with the STL, what kinds aren't, and why?
What techniques are available for writing powerful generic template code that can change its
own behavior based on the capabilities of the types it's given to work with?
When and how should you optimize your code? Why can (and do) fancy optimizations get us into
trouble? And how can some of these answers change if you're writing multithread-safe code?
Does exception safety affect class design, or can it be retrofitted in as an afterthought?
How can you avoid the Siamese Twin problem when combining inheritance-based libraries from
different vendors?
How can you safely use auto_ptr, and then use common design patterns to adapt it to avoid
common pitfalls? Can you use auto_ptr as a class member? What must you know before you
I l@ve RuBoard
elect to use it that way?
Plus one of the most frequently recurring questions about modern C++: When and how should
you use namespaces, anyway?
A must-have for the serious programmer, More Exceptional C++ provides a thorough and pragmatic
understanding of the language while showing you how to write exceptional code in C++.
I l@ve RuBoard
I l@ve RuBoard
Table of Contents
Table of Contents
•
•
More Exceptional C++
More Exceptional C++
By Herb Sutter
By Herb Sutter
Publisher
Publisher
Pub Date
Pub Date
ISBN
ISBN
Pages
Pages
: Addison Wesley
: Addison Wesley
: December 17, 2001
: December 17, 2001
: 0-201-70434-X
: 0-201-70434-X
: 304
: 304
Copyright
Foreword
Preface
Generic Programming and the C++ Standard Library
What's "More?"
What I Assume You Know
How to Read This Book
Namespaces, Typename, References, and Other Conventions
Acknowledgments
Organized in a practical problem-and-solution format, More Exceptional C++ picks up where the
widely acclaimed Exceptional C++ leaves off, providing successful strategies for solving real-world
problems in C++. Drawing from years of in-the-trenches experience, Herb Sutter provides tested
techniques and practical solutions for programmers designing modern software systems with C++,
from small projects to enterprise applications.
Built around forty programming puzzles, More Exceptional C++ helps you understand the rules and
issues critical to successful software design and development in C++. New themes included in this
sequel place a strong emphasis on generic programming, memory management, and using the C++
standard library, including coverage of important techniques like traits and predicates. Also included
are guidelines and considerations to remember when using standard containers and algorithms--topics
rarely covered in-depth in other sources.
Readers will find solutions to such important questions as:
Item 2. Predicates, Part 1: What remove() Removes
What pitfalls might you encounter when using std::map and std::set, and how can you safely
avoid them?
Solution
When and how should you optimize your code? Why can (and do) fancy optimizations get us into
trouble? And how can some of these answers change if you're writing multithread-safe code?
How can you avoid the Siamese Twin problem when combining inheritance-based libraries from
different vendors?
What techniques are available for writing powerful generic template code that can change its
own behavior based on the capabilities of the types it's given to work with?
Solution
How can you safely use auto_ptr, and then use common design patterns to adapt it to avoid
common pitfalls? Can you use auto_ptr as a class member? What must you know before you
Does exception safety affect class design, or can it be retrofitted in as an afterthought?
Item 6. Containers, Pointers, and Containers That Aren't
What kinds of predicates are safe to use with the STL, what kinds aren't, and why?
Item 4. Extensible Templates: Via Inheritance or Traits?
Item 7. Using vector and deque
Item 3. Predicates, Part 2: Matters of State
Item 5. Typename
Solution
Solution
Solution
Item 1. Switching Streams
Solution
Solution
Item 8. Using set and map
Solution
Item 9. Equivalent Code?
Item 11. Mastermind
Solution
Solution
Solution
Solution
Item 12. inline
Solution
Table of Contents
Item 10. Template Specialization and Overloading
Item 13. Lazy Optimization, Part 1: A Plain Old String
Item 14. Lazy Optimization, Part 2: Introducing Laziness
Item 15. Lazy Optimization, Part 3: Iterators and References
Simplified Rules Summary
Solution
Item 16. Lazy Optimization, Part 4: Multithreaded Environments
: Addison Wesley
Solution
: December 17, 2001
: 0-201-70434-X
: 304
Solution
I l@ve RuBoard
•
Optimization and Performance
More Exceptional C++
By Herb Sutter
Publisher
Pub Date
ISBN
Pages
Organized in a practical problem-and-solution format, More Exceptional C++ picks up where the
widely acclaimed Exceptional C++ leaves off, providing successful strategies for solving real-world
problems in C++. Drawing from years of in-the-trenches experience, Herb Sutter provides tested
techniques and practical solutions for programmers designing modern software systems with C++,
from small projects to enterprise applications.
Built around forty programming puzzles, More Exceptional C++ helps you understand the rules and
issues critical to successful software design and development in C++. New themes included in this
sequel place a strong emphasis on generic programming, memory management, and using the C++
standard library, including coverage of important techniques like traits and predicates. Also included
are guidelines and considerations to remember when using standard containers and algorithms--topics
rarely covered in-depth in other sources.
Readers will find solutions to such important questions as:
How can you avoid the Siamese Twin problem when combining inheritance-based libraries from
different vendors?
Inheritance and Polymorphism
How can you safely use auto_ptr, and then use common design patterns to adapt it to avoid
common pitfalls? Can you use auto_ptr as a class member? What must you know before you
When and how should you optimize your code? Why can (and do) fancy optimizations get us into
trouble? And how can some of these answers change if you're writing multithread-safe code?
What pitfalls might you encounter when using std::map and std::set, and how can you safely
avoid them?
Solution
Item 20. An Unmanaged Pointer Problem, Part 1: Parameter Evaluation
What techniques are available for writing powerful generic template code that can change its
own behavior based on the capabilities of the types it's given to work with?
Solution
Item 21. An Unmanaged Pointer Problem, Part 2: What About auto_ptr?
Does exception safety affect class design, or can it be retrofitted in as an afterthought?
Item 23. Exception-Safe Class Design, Part 2: Inheritance
What kinds of predicates are safe to use with the STL, what kinds aren't, and why?
Item 22. Exception-Safe Class Design, Part 1: Copy Assignment
Item 17. Constructor Failures, Part 1: Object Lifetimes
Item 18. Constructor Failures, Part 2: Absorption?
Exception Safety Issues and Techniques
Item 24. Why Multiple Inheritance?
Solution
Solution
Solution
Solution
Solution
Item 19. Uncaught Exceptions
Solution
Solution
Solution
Free Functions and Macros
Solution
Solution
Solution
Item 32. Recursive Declarations
Item 28. Controlled Polymorphism
Memory and Resource Management
Item 27. (Im)pure Virtual Functions
Table of Contents
Item 25. Emulating Multiple Inheritance
Item 26. Multiple Inheritance and the Siamese Twin Problem
Item 31. Smart Pointer Members, Part 2: Toward a ValuePtr
Item 30. Smart Pointer Members, Part 1: A Problem with auto_ptr
: Addison Wesley
Solution
: December 17, 2001
: 0-201-70434-X
: 304
Solution
I l@ve RuBoard
•
Item 29. Using auto_ptr
More Exceptional C++
By Herb Sutter
Publisher
Pub Date
ISBN
Pages
Organized in a practical problem-and-solution format, More Exceptional C++ picks up where the
widely acclaimed Exceptional C++ leaves off, providing successful strategies for solving real-world
problems in C++. Drawing from years of in-the-trenches experience, Herb Sutter provides tested
techniques and practical solutions for programmers designing modern software systems with C++,
from small projects to enterprise applications.
Built around forty programming puzzles, More Exceptional C++ helps you understand the rules and
issues critical to successful software design and development in C++. New themes included in this
sequel place a strong emphasis on generic programming, memory management, and using the C++
standard library, including coverage of important techniques like traits and predicates. Also included
are guidelines and considerations to remember when using standard containers and algorithms--topics
rarely covered in-depth in other sources.
Readers will find solutions to such important questions as:
How can you safely use auto_ptr, and then use common design patterns to adapt it to avoid
common pitfalls? Can you use auto_ptr as a class member? What must you know before you
Afterword
When and how should you optimize your code? Why can (and do) fancy optimizations get us into
trouble? And how can some of these answers change if you're writing multithread-safe code?
Item 40. Namespaces, Part 2: Migrating to Namespaces
How can you avoid the Siamese Twin problem when combining inheritance-based libraries from
different vendors?
What pitfalls might you encounter when using std::map and std::set, and how can you safely
Solution
avoid them?
What techniques are available for writing powerful generic template code that can change its
own behavior based on the capabilities of the types it's given to work with?
Item 38. Typedef
What kinds of predicates are safe to use with the STL, what kinds aren't, and why?
Item 37. Forward Declarations
Does exception safety affect class design, or can it be retrofitted in as an afterthought?
Item 39. Namespaces, Part 1: Using-Declarations and Using-Directives
Item 33. Simulating Nested Functions
Miscellaneous Topics
Item 36. Initialization
Item 35. #Definition
Solution
Solution
Solution
Item 34. Preprocessor Macros
Solution
Solution
Solution
Solution
Solution
Appendix A. Optimizations That Aren't (in a Multithreaded World)
Introduction
What's the Fuss About?
Recap: Plain Old Original:: String (Item 13)
Using a Plain Old String: Single-Threaded
Using a Plain Old String: Multithreaded
Now Throw in an "Optimization": Copy-On-Write (COW)
Using a COW String: Multithreaded
Mild versus Severe Inefficiencies
Some Actual Numbers
A Real-World Example: C++'s basic_string
"Am I Affected?"
Summary
Table of Contents
Appendix B. Test Results for Single-Threaded versus Multithread-Safe String Implementations
I l@ve RuBoard
•
More Exceptional C++
By Herb Sutter
Publisher
Pub Date
ISBN
I l@ve RuBoard
Pages
: Addison Wesley
: December 17, 2001
: 0-201-70434-X
: 304
Approach
Raw Measurements
Test Harness
Bibliography
Organized in a practical problem-and-solution format, More Exceptional C++ picks up where the
widely acclaimed Exceptional C++ leaves off, providing successful strategies for solving real-world
problems in C++. Drawing from years of in-the-trenches experience, Herb Sutter provides tested
techniques and practical solutions for programmers designing modern software systems with C++,
from small projects to enterprise applications.
Built around forty programming puzzles, More Exceptional C++ helps you understand the rules and
issues critical to successful software design and development in C++. New themes included in this
sequel place a strong emphasis on generic programming, memory management, and using the C++
standard library, including coverage of important techniques like traits and predicates. Also included
are guidelines and considerations to remember when using standard containers and algorithms--topics
rarely covered in-depth in other sources.
Readers will find solutions to such important questions as:
What pitfalls might you encounter when using std::map and std::set, and how can you safely
avoid them?
What kinds of predicates are safe to use with the STL, what kinds aren't, and why?
What techniques are available for writing powerful generic template code that can change its
own behavior based on the capabilities of the types it's given to work with?
When and how should you optimize your code? Why can (and do) fancy optimizations get us into
trouble? And how can some of these answers change if you're writing multithread-safe code?
Does exception safety affect class design, or can it be retrofitted in as an afterthought?
How can you avoid the Siamese Twin problem when combining inheritance-based libraries from
different vendors?
How can you safely use auto_ptr, and then use common design patterns to adapt it to avoid
common pitfalls? Can you use auto_ptr as a class member? What must you know before you
I l@ve RuBoard
I l@ve RuBoard
Copyright
Many of the designations used by manufacturers and sellers to distinguish their products are claimed
as trademarks. Where those designations appear in this book, and Addison-Wesley, Inc. was aware of
a trademark claim, the designations have been printed with initial capital letters or in all capitals.
Table of Contents
The author and publisher have taken care in the preparation of this book, but make no expressed or
implied warranty of any kind and assume no responsibility for errors or omissions. No liability is
assumed for incidental or consequential damages in connection with or arising out of the use of the
information or programs contained herein.
The publisher offers discounts on this book when ordered in quantity for special sales. For more
•
information, please contact
More Exceptional C++
By Herb Sutter
Pearson Education Corporate Sales Division
201 W. 103rd Street
: Addison Wesley
Publisher
Pub Date
: December 17, 2001
Indianapolis, IN 46290
ISBN
: 0-201-70434-X
: 304
Pages
(800) 428-5331
corpsales@pearsoned.com
Visit AW on the Web: www.aw.com/cseng/
Organized in a practical problem-and-solution format, More Exceptional C++ picks up where the
widely acclaimed Exceptional C++ leaves off, providing successful strategies for solving real-world
Library of Congress Cataloging-in-Publication Data
problems in C++. Drawing from years of in-the-trenches experience, Herb Sutter provides tested
techniques and practical solutions for programmers designing modern software systems with C++,
Sutter, Herb.
from small projects to enterprise applications.
More exceptional C++ : 40 new engineering puzzles, programming problems, and solutions / Herb
Built around forty programming puzzles, More Exceptional C++ helps you understand the rules and
Sutter.
issues critical to successful software design and development in C++. New themes included in this
sequel place a strong emphasis on generic programming, memory management, and using the C++
p. cm—(The C++ in-depth series)
standard library, including coverage of important techniques like traits and predicates. Also included
are guidelines and considerations to remember when using standard containers and algorithms--topics
Includes bibliographical references and index.
rarely covered in-depth in other sources.
ISBN 0-201-70434-X
Readers will find solutions to such important questions as:
1. C++ (Computer program language) I. Title. II. Series.
QA76.73.C153 S89 2002
What pitfalls might you encounter when using std::map and std::set, and how can you safely
avoid them?
005.13'3—dc21
What kinds of predicates are safe to use with the STL, what kinds aren't, and why?
2001046436
What techniques are available for writing powerful generic template code that can change its
own behavior based on the capabilities of the types it's given to work with?
Copyright © 2002 Pearson Education, Inc.
When and how should you optimize your code? Why can (and do) fancy optimizations get us into
All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or
trouble? And how can some of these answers change if you're writing multithread-safe code?
transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording, or
otherwise, without the prior consent of the publisher. Printed in the United States of America.
Does exception safety affect class design, or can it be retrofitted in as an afterthought?
Published simultaneously in Canada.
For information on obtaining permission for use of material from this work, please submit a written
request to
How can you avoid the Siamese Twin problem when combining inheritance-based libraries from
different vendors?
Pearson Education, Inc.
How can you safely use auto_ptr, and then use common design patterns to adapt it to avoid
common pitfalls? Can you use auto_ptr as a class member? What must you know before you
Rights and Contracts Department
I l@ve RuBoard
75 Arlington Street, Suite 300
Boston, MA 02116
Fax: (617) 848-7047
Text printed on recycled paper
2 3 4 5 6 7 8 9 10—CRS—0605040302
Second printing, February 2002
Table of Contents
Dedication
•
More Exceptional C++
For G ünter and Elisabeth
By Herb Sutter
I l@ve RuBoard
Publisher
Pub Date
ISBN
Pages
: Addison Wesley
: December 17, 2001
: 0-201-70434-X
: 304
Organized in a practical problem-and-solution format, More Exceptional C++ picks up where the
widely acclaimed Exceptional C++ leaves off, providing successful strategies for solving real-world
problems in C++. Drawing from years of in-the-trenches experience, Herb Sutter provides tested
techniques and practical solutions for programmers designing modern software systems with C++,
from small projects to enterprise applications.
Built around forty programming puzzles, More Exceptional C++ helps you understand the rules and
issues critical to successful software design and development in C++. New themes included in this
sequel place a strong emphasis on generic programming, memory management, and using the C++
standard library, including coverage of important techniques like traits and predicates. Also included
are guidelines and considerations to remember when using standard containers and algorithms--topics
rarely covered in-depth in other sources.
Readers will find solutions to such important questions as:
What pitfalls might you encounter when using std::map and std::set, and how can you safely
avoid them?
What kinds of predicates are safe to use with the STL, what kinds aren't, and why?
What techniques are available for writing powerful generic template code that can change its
own behavior based on the capabilities of the types it's given to work with?
When and how should you optimize your code? Why can (and do) fancy optimizations get us into
trouble? And how can some of these answers change if you're writing multithread-safe code?
Does exception safety affect class design, or can it be retrofitted in as an afterthought?
How can you avoid the Siamese Twin problem when combining inheritance-based libraries from
different vendors?
How can you safely use auto_ptr, and then use common design patterns to adapt it to avoid
common pitfalls? Can you use auto_ptr as a class member? What must you know before you