I l@ve RuBoard
Table of Contents
•
Exceptional C++: 47 Engineering Puzzles,
Programming Problems, and Solutions
By Herb Sutter
Publisher
Pub Date
ISBN
Pages
: Addison Wesley
: November 18, 1999
: 0-201-61562-2
: 240
Exceptional C++ shows by example how to go about sound software engineering in standard C++. Do
you enjoy solving thorny C++ problems and puzzles? Do you relish writing robust and extensible
code? Then take a few minutes and challenge yourself with some tough C++ design and programming
problems.
The puzzles and problems in Exceptional C++ not only entertain, they will help you hone your skills to
become the sharpest C++ programmer you can be. Many of these problems are culled from the
famous Guru of the Week feature of the Internet newsgroup comp.lang.c++.moderated, expanded
and updated to conform to the official ISO/ANSI C++ Standard.
Each problem is rated according to difficulty and is designed to illustrate subtle programming mistakes
or design considerations. After you've had a chance to attempt a solution yourself, the book then
dissects the code, illustrates what went wrong, and shows how the problem can be fixed. Covering a
broad range of C++ topics, the problems and solutions address critical issues such as:
Generic programming and how to write reusable templates
Exception safety issues and techniques
Robust class design and inheritance
Compiler firewalls and the Pimpl Idiom
Name lookup, namespaces, and the Interface Principle
Memory management issues and techniques
Traps, pitfalls, and anti-idioms
Optimization
Try your skills against the C++ masters and come away with the insight and experience to create
more efficient, effective, robust, and portable C++ code.
I l@ve RuBoard
I l@ve RuBoard
I l@ve RuBoard
Table of Contents
Table of Contents
•
•
Exceptional C++: 47 Engineering Puzzles,
Exceptional C++: 47 Engineering Puzzles,
Programming Problems, and Solutions
Programming Problems, and Solutions
By Herb Sutter
By Herb Sutter
Publisher
Publisher
Pub Date
Pub Date
ISBN
ISBN
Pages
Pages
: Addison Wesley
: Addison Wesley
: November 18, 1999
: November 18, 1999
: 0-201-61562-2
: 0-201-61562-2
: 240
: 240
Copyright
Foreword
Preface
Item 1. Iterators
Solution
Item 3. Case-Insensitive Strings—Part 2
Item 2. Case-Insensitive Strings—Part 1
Generic Programming and the C++ Standard Library
How to Read This Book
How We Got Here: GotW and PeerDirect
Acknowledgments
Exceptional C++ shows by example how to go about sound software engineering in standard C++. Do
you enjoy solving thorny C++ problems and puzzles? Do you relish writing robust and extensible
code? Then take a few minutes and challenge yourself with some tough C++ design and programming
problems.
The puzzles and problems in Exceptional C++ not only entertain, they will help you hone your skills to
become the sharpest C++ programmer you can be. Many of these problems are culled from the
famous Guru of the Week feature of the Internet newsgroup comp.lang.c++.moderated, expanded
and updated to conform to the official ISO/ANSI C++ Standard.
Each problem is rated according to difficulty and is designed to illustrate subtle programming mistakes
or design considerations. After you've had a chance to attempt a solution yourself, the book then
dissects the code, illustrates what went wrong, and shows how the problem can be fixed. Covering a
broad range of C++ topics, the problems and solutions address critical issues such as:
Try your skills against the C++ masters and come away with the insight and experience to create
Solution
Alternative: The Standard Library Approach
Generic programming and how to write reusable templates
Exception safety issues and techniques
Item 4. Maximally Reusable Generic Containers—Part 1
Name lookup, namespaces, and the Interface Principle
Item 5. Maximally Reusable Generic Containers—Part 2
Compiler firewalls and the Pimpl Idiom
Item 7. Using the Standard Library (or, Temporaries Revisited)
Memory management issues and techniques
Item 6. Temporary Objects
Robust class design and inheritance
Traps, pitfalls, and anti-idioms
Solution
Solution
Solution
Solution
Optimization
Solution
Solution
Solution
Solution
Solution
Solution
Table of Contents
Solution
Exception-Safety Issues and Techniques
Item 9. Writing Exception-Safe Code—Part 2
Item 8. Writing Exception-Safe Code—Part 1
Item 16. Writing Exception-Safe Code—Part 9
Item 15. Writing Exception-Safe Code—Part 8
Item 14. Writing Exception-Safe Code—Part 7
Item 13. Writing Exception-Safe Code—Part 6
Item 12. Writing Exception-Safe Code—Part 5
Item 11. Writing Exception-Safe Code—Part 4
Item 10. Writing Exception-Safe Code—Part 3
Solution
: Addison Wesley
: November 18, 1999
Solution
: 0-201-61562-2
: 240
Solution
I l@ve RuBoard
•
Exceptional C++: 47 Engineering Puzzles,
Programming Problems, and Solutions
By Herb Sutter
Publisher
Pub Date
ISBN
Pages
Exceptional C++ shows by example how to go about sound software engineering in standard C++. Do
you enjoy solving thorny C++ problems and puzzles? Do you relish writing robust and extensible
code? Then take a few minutes and challenge yourself with some tough C++ design and programming
problems.
The puzzles and problems in Exceptional C++ not only entertain, they will help you hone your skills to
become the sharpest C++ programmer you can be. Many of these problems are culled from the
famous Guru of the Week feature of the Internet newsgroup comp.lang.c++.moderated, expanded
and updated to conform to the official ISO/ANSI C++ Standard.
Each problem is rated according to difficulty and is designed to illustrate subtle programming mistakes
or design considerations. After you've had a chance to attempt a solution yourself, the book then
dissects the code, illustrates what went wrong, and shows how the problem can be fixed. Covering a
broad range of C++ topics, the problems and solutions address critical issues such as:
Try your skills against the C++ masters and come away with the insight and experience to create
Generic programming and how to write reusable templates
Class Design and Inheritance
Item 20. Class Mechanics
Name lookup, namespaces, and the Interface Principle
Item 23. Class Relationships—Part 2
Item 21. Overriding Virtual Functions
Exception safety issues and techniques
Item 24. Uses and Abuses of Inheritance
Traps, pitfalls, and anti-idioms
Memory management issues and techniques
Compiler firewalls and the Pimpl Idiom
Solution
Item 17. Writing Exception-Safe Code—Part 10
Robust class design and inheritance
Item 22. Class Relationships—Part 1
Item 25. Object-Oriented Programming
Item 18. Code Complexity—Part 1
Solution
Solution
Solution
Solution
Solution
Item 19. Code Complexity—Part 2
Solution
Solution
Optimization
Solution
Compiler Firewalls and the Pimpl Idiom
Solution
Item 29. Compilation Firewalls
Item 30. The "Fast Pimpl" Idiom
Solution
Solution
Solution
Solution
Table of Contents
Name Lookup, Namespaces, and the Interface Principle
Item 28. Minimizing Compile-time Dependencies—Part 3
Item 27. Minimizing Compile-time Dependencies—Part 2
Item 26. Minimizing Compile-time Dependencies—Part 1
Item 33. Name Lookup and the Interface Principle—Part 3
Item 32. Name Lookup and the Interface Principle—Part 2
Item 31. Name Lookup and the Interface Principle—Part 1
: Addison Wesley
: November 18, 1999
Solution
: 0-201-61562-2
: 240
Solution
I l@ve RuBoard
•
Exceptional C++: 47 Engineering Puzzles,
Programming Problems, and Solutions
By Herb Sutter
Publisher
Pub Date
ISBN
Pages
Exceptional C++ shows by example how to go about sound software engineering in standard C++. Do
you enjoy solving thorny C++ problems and puzzles? Do you relish writing robust and extensible
code? Then take a few minutes and challenge yourself with some tough C++ design and programming
problems.
The puzzles and problems in Exceptional C++ not only entertain, they will help you hone your skills to
become the sharpest C++ programmer you can be. Many of these problems are culled from the
famous Guru of the Week feature of the Internet newsgroup comp.lang.c++.moderated, expanded
and updated to conform to the official ISO/ANSI C++ Standard.
Each problem is rated according to difficulty and is designed to illustrate subtle programming mistakes
or design considerations. After you've had a chance to attempt a solution yourself, the book then
dissects the code, illustrates what went wrong, and shows how the problem can be fixed. Covering a
broad range of C++ topics, the problems and solutions address critical issues such as:
Try your skills against the C++ masters and come away with the insight and experience to create
Generic programming and how to write reusable templates
Memory management issues and techniques
Item 40. Object Lifetimes—Part 1
Name lookup, namespaces, and the Interface Principle
Exception safety issues and techniques
Traps, Pitfalls, and Anti-Idioms
Item 38. Object Identity
Robust class design and inheritance
Item 34. Name Lookup and the Interface Principle—Part 4
Compiler firewalls and the Pimpl Idiom
Item 35. Memory Management—Part 1
Solution
Traps, pitfalls, and anti-idioms
Item 37. AUTO_PTR
Solution
Item 36. Memory Management—Part 2
Solution
Solution
Solution
Solution
Item 41. Object Lifetimes—Part 2
Optimization
Solution
Memory Management
Solution
Solution
Item 39. Automatic Conversions
Solution
Solution
Solution
Item 44. Casts
Solution
Miscellaneous Topics
Item 43. Const-Correctness
Item 42. Variable Initialization—Or is it?
I l@ve RuBoard
•
Exceptional C++: 47 Engineering Puzzles,
Programming Problems, and Solutions
By Herb Sutter
Publisher
Pub Date
ISBN
Pages
I l@ve RuBoard
Solution
: Addison Wesley
: November 18, 1999
: 0-201-61562-2
: 240
Table of Contents
Item 46. Forwarding Functions
Item 47. Control Flow
Item 45. BOOL
Solution
Afterword
Bibliography
Exceptional C++ shows by example how to go about sound software engineering in standard C++. Do
you enjoy solving thorny C++ problems and puzzles? Do you relish writing robust and extensible
code? Then take a few minutes and challenge yourself with some tough C++ design and programming
problems.
The puzzles and problems in Exceptional C++ not only entertain, they will help you hone your skills to
become the sharpest C++ programmer you can be. Many of these problems are culled from the
famous Guru of the Week feature of the Internet newsgroup comp.lang.c++.moderated, expanded
and updated to conform to the official ISO/ANSI C++ Standard.
Each problem is rated according to difficulty and is designed to illustrate subtle programming mistakes
or design considerations. After you've had a chance to attempt a solution yourself, the book then
dissects the code, illustrates what went wrong, and shows how the problem can be fixed. Covering a
broad range of C++ topics, the problems and solutions address critical issues such as:
Generic programming and how to write reusable templates
Exception safety issues and techniques
Robust class design and inheritance
Compiler firewalls and the Pimpl Idiom
Name lookup, namespaces, and the Interface Principle
Memory management issues and techniques
Traps, pitfalls, and anti-idioms
Optimization
Try your skills against the C++ masters and come away with the insight and experience to create
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 Longman, Inc., was
aware of a trademark claim, the designations have been printed in initial capital letters or all capital
letters.
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
Exceptional C++: 47 Engineering Puzzles,
information, please contact:
Programming Problems, and Solutions
Pearson Education Corporate Sales Division
By Herb Sutter
201 W. 103rd Street
Publisher
: Addison Wesley
: November 18, 1999
Pub Date
Indianapolis, IN 46290
ISBN
: 0-201-61562-2
(800) 428-5331
: 240
Pages
corpsales@pearsoned.com
Library of Congress Cataloging-in-Publication Data
Exceptional C++ shows by example how to go about sound software engineering in standard C++. Do
you enjoy solving thorny C++ problems and puzzles? Do you relish writing robust and extensible
Sutter, Herb.
code? Then take a few minutes and challenge yourself with some tough C++ design and programming
Exceptional C++ : 47 engineering puzzles, programming problems, and solutions / Herb Sutter.
problems.
p. cm.
The puzzles and problems in Exceptional C++ not only entertain, they will help you hone your skills to
become the sharpest C++ programmer you can be. Many of these problems are culled from the
Includes bibliographical references and index.
famous Guru of the Week feature of the Internet newsgroup comp.lang.c++.moderated, expanded
and updated to conform to the official ISO/ANSI C++ Standard.
ISBN 0-201-61562-2 (alk. paper)
Each problem is rated according to difficulty and is designed to illustrate subtle programming mistakes
1. C++ (Computer program language) I. Title.
or design considerations. After you've had a chance to attempt a solution yourself, the book then
dissects the code, illustrates what went wrong, and shows how the problem can be fixed. Covering a
QA76.73.C153.S88 1999
broad range of C++ topics, the problems and solutions address critical issues such as:
005.13'3—dc21 99-046115
Generic programming and how to write reusable templates
CIP
Exception safety issues and techniques
Copyright © 2000 by Addison Wesley Longman, Inc.
Robust class design and inheritance
All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or
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.
Published simultaneously in Canada.
Compiler firewalls and the Pimpl Idiom
Name lookup, namespaces, and the Interface Principle
Credits
Memory management issues and techniques
Traps, pitfalls, and anti-idioms
Project Editor: Deborah Lafferty
Optimization
Production Manager: Sarah Weaver
Try your skills against the C++ masters and come away with the insight and experience to create
Design and Composition: Kim Arney
I l@ve RuBoard
Index: Michael Loo
Text printed on recycled paper.
4 5 6 7 8 9 10—CRS—04030201
4th printing, November 2001
Dedication
Table of Contents
"extended family " for the past four years:
To my family for their patient support, and to Eric Wilson, Jeff Sumner, Juana Chang, Larry Palmer,
and the rest of the core PeerDirect development team, my
Declan West
•
Exceptional C++: 47 Engineering Puzzles,
Doug Shelley
Programming Problems, and Solutions
Duk Loi
By Herb Sutter
Ian Long
Publisher
Justin Karabegovic
Pub Date
ISBN
Kim Nguyen
Pages
: Addison Wesley
: November 18, 1999
: 0-201-61562-2
: 240
Margot Fulcher
Mark Cheers
Exceptional C++ shows by example how to go about sound software engineering in standard C++. Do
Morgan Jones
you enjoy solving thorny C++ problems and puzzles? Do you relish writing robust and extensible
code? Then take a few minutes and challenge yourself with some tough C++ design and programming
Violetta Lukow
problems.
Thanks for everything, guys.
The puzzles and problems in Exceptional C++ not only entertain, they will help you hone your skills to
become the sharpest C++ programmer you can be. Many of these problems are culled from the
I l@ve RuBoard
famous Guru of the Week feature of the Internet newsgroup comp.lang.c++.moderated, expanded
and updated to conform to the official ISO/ANSI C++ Standard.
Each problem is rated according to difficulty and is designed to illustrate subtle programming mistakes
or design considerations. After you've had a chance to attempt a solution yourself, the book then
dissects the code, illustrates what went wrong, and shows how the problem can be fixed. Covering a
broad range of C++ topics, the problems and solutions address critical issues such as:
Generic programming and how to write reusable templates
Exception safety issues and techniques
Robust class design and inheritance
Compiler firewalls and the Pimpl Idiom
Name lookup, namespaces, and the Interface Principle
Memory management issues and techniques
Traps, pitfalls, and anti-idioms
Optimization
Try your skills against the C++ masters and come away with the insight and experience to create