More Exceptional C++: 40 New Engineering Puzzles, Programming Problems, and Solutions / Edition 1

Paperback (Print)
Used and New from Other Sellers
Used and New from Other Sellers
from $8.94
Usually ships in 1-2 business days
(Save 82%)
Other sellers (Paperback)
  • All (14) from $8.94   
  • New (7) from $31.81   
  • Used (7) from $8.94   

Overview

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 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++.

Read More Show Less

Product Details

  • ISBN-13: 9780201704341
  • Publisher: Addison-Wesley
  • Publication date: 12/17/2001
  • Series: C++ In-Depth Series
  • Edition description: New Edition
  • Edition number: 1
  • Pages: 304
  • Product dimensions: 7.36 (w) x 9.20 (h) x 0.62 (d)

Meet the Author

Herb Sutter is the author of three highly acclaimed books, Exceptional C++ Style, Exceptional C++, and More Exceptional C++ (Addison-Wesley). He chairs the ISO C++ standards committee, and is contributing editor and columnist for C/C++ Users Journal. As a software architect for Microsoft, Sutter leads the design of C++ language extensions for .NET programming.

Read More Show Less

Read an Excerpt

The Greek philosopher Socrates taught by asking his students questions—questions designed to guide them and help them draw conclusions from what they already knew, and to show them how the things they were learning related to each other and to their existing knowledge. This method has become so famous that we now call it the "Socratic method." From our point of view as students, Socrates' approach involves us, makes us think, and helps us relate and apply what we already know to new information.

This book takes a page from Socrates, as did its predecessor, Exceptional C++ Sutter00. It assumes you're involved in some aspect of writing production C++ software today, and uses a question-answer format to teach you how to make effective use of standard C++ and its standard library with a particular focus on sound software engineering in modern C++. Many of the problems are drawn directly from experiences I and others have encountered while working with production C++ code. The goal of the questions is to help you draw conclusions from things you already know as well as things you've just learned, and to show how they interrelate. The puzzles will show how to reason about C++ design and programming issues—some of them common issues, some not so common; some of them plain issues, some more esoteric; and a couple because, well, just because they're fun.

This book is about all aspects of C++. I don't mean to say that it touches on every detail of C++—that would require many more pages—but rather that it draws from the wide palette of the C++ language and library features to show how apparently unrelated items can be used together to synthesize novel solutions to common problems. It also shows how apparently unrelated parts of the palette interrelate on their own, even when you don't want them to, and what to do about it. You will find material here about templates and namespaces, exceptions and inheritance, solid class design and design patterns, generic programming and macro magic—and not just as randomized tidbits, but as cohesive Items showing the interrelationships among all of these parts of modern C++.

What's "More?"

More Exceptional C++ continues where Exceptional C++ left off. This book follows in the tradition of the first: It delivers new material, organized in bite-sized Items and grouped into themed sections. Readers of the first book will find some familiar section themes, now including new material, such as exception safety, generic programming, and memory management techniques. The two books overlap in structure and theme, not in content.

Where else does More Exceptional C++ differ? This book has a much stronger emphasis on generic programming and on using the C++ standard library effectively, including coverage of important techniques such as traits and predicates. Several Items provide in-depth looks at considerations to keep in mind when using the standard containers and algorithms; many of these considerations I've not seen covered elsewhere. There's a new section and two appendixes that focus on optimization in single- and multithreaded environments—issues that are now more than ever of practical consequence for development shops writing production code.

Versions of most Items originally appeared in Internet and magazine columns, particularly as Guru of the Week GotW issues #31 to 62, and as print columns and articles I've written for C/C++ Users Journal, Dr. Dobb's Journal, the former C++ Report, and other publications. The material in this book has been significantly revised, expanded, corrected, and updated since those initial versions, and this book (along with its de rigueur errata list available at www.gotw.ca) should be treated as the current and authoritative version of that original material.What I Assume You Know

I expect that you already know the basics of C++. If you don't, start with a good C++ introduction and overview. Good choices are a classic tome like Bjarne Stroustrup's The C++ Programming Language Stroustrup00, or Stan Lippman and Josee Lajoie's C++ Primer, Third Edition Lippman98. Next, be sure to pick up a style guide such as Scott Meyers' classic Effective C++ books Meyers96 Meyers97. I find the browser-based CD version Meyers99 convenient and useful.

How to Read This Book

Each Item in this book is presented as a puzzle or problem, with an introductory header that resembles the following:

Item #: The Topic of This Puzzle
Difficulty: X

The topic tag and difficulty rating gives you a hint of what you're in for. Note that the difficulty rating is my subjective guess at how difficult I expect most people will find each problem, so you may well find that a "7" problem is easier for you than some "5" problem. Since writing Exceptional C++, I've regularly received e-mail saying that "Item #N is easier (or harder) than that!" It's common for different people to vote "easier!" and "harder!" for the same Item. Ratings are personal; any Item's actual difficulty for you really depends on your knowledge and experience and could be easier or harder for someone else. In most cases, though, you should find the rating to be a good rule-of-thumb guide to what to expect.

You might choose to read the whole book front to back; that's great, but you don't have to. You might decide to read all the Items in a section together because you're particularly interested in that section's topic; that's cool, too. Except where there are what I call a "miniseries" of related problems which you'll see designated as "Part 1," "Part 2," and so on, the Items are pretty independent, and you should feel free to jump around, following the many cross-references among the Items in the book, as well as some references to Exceptional C++. The only guidance I'll offer is that the miniseries are designed to be read consecutively as a group; other than that, the choice is yours.

Namespaces, Typename, References, and Other Conventions

I make quite a few recommendations in this book, and I won't give you guidelines that tell you to do something I don't already do myself. That includes what I do in my own example code throughout this book. I'll also bow to existing practice and modern style, even when it really makes no material difference.

On that note, a word about namespaces: In the code examples, if you see a using-directive at file scope in one example and at function scope in another example a few pages or Items later, there's no deeper reason than that's what felt right and aesthetically pleasing to me for that particular case; for the rationale, turn to Item 40. In the narrative text itself, I've chosen to qualify standard library names with

std:: when I want to emphasize that it's the standard facility I'm talking about. Once that's established, I'll generally switch back to using the unqualified name.

When it comes to declaring template parameters, I sometimes come across people who think that writing class instead of typename is old-fashioned, even though there's no functional difference between the two and the standard itself uses class most everywhere. Purely for style, and to emphasize that this book is about today's modern C++, I've switched to using typename instead of class to declare template parameters. The only exception is one place in Item 33, where I quote directly from the standard; the standard says class, so I left it in there.

Unless I call something a "complete program," it's probably not. Remember that the code examples are usually just snippets or partial programs and aren't expected to compile in isolation. You'll usually have to provide some obvious scaffolding to make a complete program out of the snippet shown.

Finally, a word about URLs: On the Web, stuff moves. In particular, stuff I have no control over moves. That makes it a real pain to publish random Web URLs in a print book lest they become out of date before the book makes it to the printer's, never mind after it's been sitting on your desk for five years. When I reference other people's articles or Web sites in this book, I do it via a URL on my own Web site, www.gotw.ca, which I can control and which contains just a straight redirect to the real Web page. If you find that a link printed in this book no longer works, send me e-mail and tell me; I'll update that redirector to point to the new page's location (if I can find the page again) or to say that the page no longer exists (if I can't). Either way, this book's URLs will stay up-to-date despite the rigors of print media in an Internet world. Whew.

Herb Sutter Toronto, June 2001

Read More Show Less

Table of Contents

Foreword
Preface
Generic Programming and the C++ Standard Library 1
Item 1 Switching Streams 1
Item 2 Predicates, Part 1: What remove () Removes 6
Item 3 Predicates, Part 2: Matters of State 11
Item 4 Extensible Templates: Via Inheritance or Traits? 19
Item 5 Typename 32
Item 6 Containers, Pointers, and Containers That Aren't 36
Item 7 Using Vector and Deque 46
Item 8 Using Set and Map 53
Item 9 Equivalent Code? 59
Item 10 Template Specialization and Overloading 64
Item 11 Mastermind 69
Optimization and Performance 83
Item 12 Inline 83
Item 13 Lazy Optimization, Part 1: A Plain Old String 86
Item 14 Lazy Optimization, Part 2: Introducing Laziness 90
Item 15 Lazy Optimization, Part 3: Iterators and References 94
Item 16 Lazy Optimization, Part 4: Multithreaded Environments 103
Exception Safety Issue and Techniques 115
Item 17 Constructor Failures, Part 1: Object Lifetimes 115
Item 18 Constructor Failures, Part 2: Absorption? 119
Item 19 Uncaught Exceptions 126
Item 20 An Unmanaged Pointer Problem, Part 1: Parameter Evaluation 132
Item 21 An Unmanaged Pointer Problem, Part 2: What About autoöptr? 135
Item 22 Exception-Safe Class Design, Part 1: Copy Assignment 141
Item 23 Exception-Safe Class Design, Part 2: Inheritance 149
Inheritance and Polymorphism 155
Item 24 Why Multiple Inheritance? 155
Item 25 Emulating Multiple Inheritance 159
Item 26 Multiple Inheritance and the Siamese Twin Problem 162
Item 27 (Im)pure Virtual Functions 167
Item 28 Controlled Polymorphism 172
Memory and Resource Management 175
Item 29 Using autoöptr 175
Item 30 Smart Pointer Members, Part 1: A Problem with autoöptr 182
Item 31 Smart Pointer Members, Part 2: Toward a ValuePtr 187
Free Functions and Macros 201
Item 32 Recursive Declarations 201
Item 33 Simulating Nested Functions 206
Item 34 Preprocessor Macros 215
Item 35 #Definition 218
Miscellaneous Topics 223
Item 36 Initialization 223
Item 37 Forward Declarations 226
Item 38 Typedef 228
Item 39 Namespaces, Part 1: Using-Declarations and Using-Directives 231
Item 40 Namespaces, Part 2: Migrating to Namespaces 234
Afterword 245
App. A Optimizations That Aren't (in a Multithreaded World) 247
App. B Test Results for Single-Threaded Versus Multithread-Safe String Implementations 263
Bibliography 271
Index 273
Read More Show Less

Preface

The famous Greek philosopher Socrates taught by asking his students questions—questions designed to guide them, to help them draw conclusions from what they already knew, and to show them how the new things they were learning related to each other and to their existing knowledge. This method has becomeso famous that we now call it the "socratic method" in his honor. As students, Socrates' approach involves us, it makes us think, and it helps us relate and apply what we already know to new information.

The book you're holding takes a page from Socrates, as did its predecessor Exceptional C++. This book assumes that you're involved in some aspect of writing production C++ software today, and uses a question-answer format to teach how to make effective use of standard C++ and its standard library witha particular focus on sound software engineering in modern C++. Many of the problems are drawn directly from experiences I and others have encountered while working with production C++ code. The goal of the questions? To guide, to help you draw conclusions from things you already know as well as things you've just learned, and to show how new things interrelate. The puzzles are designed to help show how to reason about C++ design and programming issues—some of them common issues, some not so common; some of them plain issues, some more esoteric; and a couple because, well, just because they're fun, so there.

This book is about all aspects of C++. I don't mean to say that this book touches on every detail of C++—that would require many more pages—but rather that it draws from the wide palette of the C++ language and library features to showhow apparently unrelated parts of the palette can be used together to synthesize novel solutions to common problems. It also shows how apparently unrelated parts of the palette interrelate all on their own even when you don't necessarily want them to, and what to do about it. You will find material here about templates and namespaces, exceptions and inheritance, solid class design and design patterns, generic programming and macro magic—and not just as randomized tidbits, but as cohesive Items showing the interrelationships between all of these parts of the whole that is modern C++.

What's "More"?

More Exceptional C++ continues where Exceptional C++ left off.

This book follows in the tradition of the first: It delivers more new material organized in bite-sized Items grouped into themed sections. Readers of the first book will find some familiar section themes, now with new material, including the themes of exception safety, generic programming, and memory management techniques. The difference here is that this material is all new since Exceptional C++; the two books overlap in structure and theme, not in content.

Where else does More Exceptional C++ differ? This book has a much stronger emphasis on generic programming and using the C++ standard library effectively, including coverage of important techniques like traits and predicates. Several Items provide in-depth looks at important considerations to remember when using the standard containers and algorithms, and many of the considerations are ones I've not yet seen covered elsewhere. There's a new section, and two appendixes, focusing on on optimization in single- and multithreaded environments—issues that are now more than ever of practical consequence for development shops writing production code.

Earlier versions of most Items originally appeared in the form of Internet and magazine columns—particularly as Guru of the Week1 issues #31-62, and as print columns and articles I've written for C/C++ Users Journal, Dr. Dobbs Journal, the former C++ Report, and other publications. The material in this book has been significantly revised, expanded, corrected, and updated since those initial versions, and this book (along with its de rigueur errata list available at www.gotw.ca) should be treated as the current and authoritative version of that original material.

What I Assume You Know

I expect that you already know the basics of C++. If you don't, start with a good C++ introduction and overview—good choices are a classic tome like Bjarne Stroustrup's The C++ Programming Language, or Stan Lippman and Josee Lajoie's C++ Primer, 3rd Edition. Next, be sure to pick up a style guide like Scott Meyers' classic Effective C++ books (I find the browser-based CD version convenient and useful).

How To Read This Book

Each item in this book is presented as a puzzle or problem, with an introductory header that looks like this:

Item ##: The Topic of This Puzzle (X / 10)

The topic tag and difficulty rating gives you a hint at what you're in for. Note that the difficulty rating is my own subjective guess at how relatively difficult I expect most people will find each problem, so you may well find that a given "7/10" problem is easier for you than another "5/10" problem. Since I wrote Exceptional C++, I've regularly received e-mail saying "Item #N is easier (or, harder) than that!"—and it's common for different people to vote "It's easier!" and "It's harder!" for the same Item. Ratings aresomewhat personal, because any Item's actual difficulty for you really depends on your existing knowledge and experience and could be easier or harder for someone else. In most cases, though, you should find the rating to be a good rule-of-thumb guide as to what to expect.

You might choose to read the whole book front to back; that's great, but you don't have to. You might decide to read all the Items in a section together because you're particularly interested in that section's topic; that's cool too. Except where there are what I call "miniseries" of related problems that you'll see designated as "Part I", "Part II", and so on, the Items are pretty independent, and you should feel free to jump around following the many cross-references among the Items in the book, as well as some references to Exceptional C++. The only guidance I'll offer is that the multi-part miniseries are designed to be read consecutively as a group; other than that, the choice is yours.

Namespaces, Typename, References, and Other Conventions

In this book, I make quite a few recommendations. In order to avoid being a hypocrite, or even looking like one, I won't give you guidelines that tell you to do something that I don't already do myself, and that includes what I do in my own example code throughout this book. I'll also bow to existing practice and modern style even when it really makes no material difference.

On that note, a word about namespaces: In the code examples, if you see me writing a using-directive at file scope in one example and at function scope in another example a few pages or Items later, there's no deeper reason than it's just because that's what felt right and aesthetically pleasing to me for thatparticular case; see Item 40 for the rationale. In the text itself, I've chosen to qualify standard library names with std:: when I want to emphasize that it's the standard facility I'm talking about, but once that's established I'll generally switch back to using the unqualified name.

When it comes to declaring template parameters, I sometimes come across people who think that writing class instead of typename is old-fashioned, even though there's no functional difference between the two and the standard itself uses class most everywhere. Purely for style, and to emphasize that this book is about today's modern C++, I've switched to using typename instead of class to declare template parameters. The only exception is one place in Item 33 where I quote directly from the standard; the standard says class, so I left it in there.

Unless I call something a "complete program," it's probably not. Remember that the code examples are usually just snippets or partial programs and aren't expected to compile in isolation. You'll usually have to provide some obvious scaffolding to make a complete program out of the snippet shown.

Finally, a word about URLs: On the Web, stuff moves. In particular, stuff I have no control over moves, which makes it problematic for me to publish random Web URLs in a print book lest they become out of date before the book makes it to the printer, never mind after it's been sitting on your desk for fiveyears. Where I reference other peoples' articles or Web sites in this book, I do it via a URL on my own Web site, www.gotw.ca, which I can control and which contains just a straight redirect to the real Web page. If you find that a link printed in this book no longer works, send me mail and tell me; I'll update that redirector to point to the new page's location (if I can find the page again) or to say that the page no longer exists (if I can't). Either way, this book's URLs will stay up to date despite the rigors of print media in an Internet world. Whew.

Herb Sutter
Toronto, June 2001
1 See http://www.gotw.ca/gotw.

Read More Show Less

Customer Reviews

Be the first to write a review
( 0 )
Rating Distribution

5 Star

(0)

4 Star

(0)

3 Star

(0)

2 Star

(0)

1 Star

(0)

Your Rating:

Your Name: Create a Pen Name or

Barnes & Noble.com Review Rules

Our reader reviews allow you to share your comments on titles you liked, or didn't, with others. By submitting an online review, you are representing to Barnes & Noble.com that all information contained in your review is original and accurate in all respects, and that the submission of such content by you and the posting of such content by Barnes & Noble.com does not and will not violate the rights of any third party. Please follow the rules below to help ensure that your review can be posted.

Reviews by Our Customers Under the Age of 13

We highly value and respect everyone's opinion concerning the titles we offer. However, we cannot allow persons under the age of 13 to have accounts at BN.com or to post customer reviews. Please see our Terms of Use for more details.

What to exclude from your review:

Please do not write about reviews, commentary, or information posted on the product page. If you see any errors in the information on the product page, please send us an email.

Reviews should not contain any of the following:

  • - HTML tags, profanity, obscenities, vulgarities, or comments that defame anyone
  • - Time-sensitive information such as tour dates, signings, lectures, etc.
  • - Single-word reviews. Other people will read your review to discover why you liked or didn't like the title. Be descriptive.
  • - Comments focusing on the author or that may ruin the ending for others
  • - Phone numbers, addresses, URLs
  • - Pricing and availability information or alternative ordering information
  • - Advertisements or commercial solicitation

Reminder:

  • - By submitting a review, you grant to Barnes & Noble.com and its sublicensees the royalty-free, perpetual, irrevocable right and license to use the review in accordance with the Barnes & Noble.com Terms of Use.
  • - Barnes & Noble.com reserves the right not to post any review -- particularly those that do not follow the terms and conditions of these Rules. Barnes & Noble.com also reserves the right to remove any review at any time without notice.
  • - See Terms of Use for other conditions and disclaimers.
Search for Products You'd Like to Recommend

Recommend other products that relate to your review. Just search for them below and share!

Create a Pen Name

Your Pen Name is your unique identity on BN.com. It will appear on the reviews you write and other website activities. Your Pen Name cannot be edited, changed or deleted once submitted.

 
Your Pen Name can be any combination of alphanumeric characters (plus - and _), and must be at least two characters long.

Continue Anonymously

    If you find inappropriate content, please report it to Barnes & Noble
    Why is this product inappropriate?
    Comments (optional)