More Books
Effective C++ 55 Specific Ways to Improve Your Programs and Designs
Effective C++ Third Edition 55 Specific Ways to Improve Your Programs and Designs
Table of Contents
Copyright
Praise for Effective C++, Third Edition
Addison-Wesley Professional Computing Series
Preface
Acknowledgments
Introduction
Terminology
Chapter 1. Accustoming Yourself to C++
Item 1: View C++ as a federation of languages
Item 2: Prefer consts, enums, and inlines to #defines
Item 3: Use const whenever possible
Item 4: Make sure that objects are initialized before they're used
Chapter 2. Constructors, Destructors, and Assignment Operators
Item 5: Know what functions C++ silently writes and calls
Item 6: Explicitly disallow the use of compiler-generated functions you do not want
Item 7: Declare destructors virtual in polymorphic base classes
Item 8: Prevent exceptions from leaving destructors
Item 9: Never call virtual functions during construction or destruction
Item 10: Have assignment operators return a reference to *this
Item 11: Handle assignment to self in operator=
Item 12: Copy all parts of an object
Chapter 3. Resource Management
Item 13: Use objects to manage resources.
Item 14: Think carefully about copying behavior in resource-managing classes.
Item 15: Provide access to raw resources in resource-managing classes.
Item 16: Use the same form in corresponding uses of new and delete.
Item 17: Store newed objects in smart pointers in standalone statements.
Chapter 4. Designs and Declarations
Item 18: Make interfaces easy to use correctly and hard to use incorrectly
Item 19: Treat class design as type design
Item 20: Prefer pass-by-reference-to-const to pass-by-value
Item 21: Don't try to return a reference when you must return an object
Item 22: Declare data members private
Item 23: Prefer non-member non-friend functions to member functions
Item 24: Declare non-member functions when type conversions should apply to all parameters
Item 25: Consider support for a non-throwing swap
Chapter 5. Implementations
Item 26: Postpone variable definitions as long as possible.
Item 27: Minimize casting.
Item 28: Avoid returning "handles" to object internals.
Item29: Strive for exception-safe code.
Item 30: Understand the ins and outs of inlining.
Item31: Minimize compilation dependencies between files.
Chapter 6. Inheritance and Object-Oriented Design
Item 32: Make sure public inheritance models "is-a."
Item 33: Avoid hiding inherited names
Item 34: Differentiate between inheritance of interface and inheritance of implementation
Item 35: Consider alternatives to virtual functions
Item 36: Never redefine an inherited non-virtual function
Item 37: Never redefine a function's inherited default parameter value
Item 38: Model "has-a" or "is-implemented-in-terms-of" through composition
Item 39: Use private inheritance judiciously
Item 40: Use multiple inheritance judiciously
Chapter 7. Templates and Generic Programming
Item 41: Understand implicit interfaces and compile-time polymorphism
Item 42: Understand the two meanings of typename
Item 43: Know how to access names in templatized base classes
Item 44: Factor parameter-independent code out of templates
Item 45: Use member function templates to accept "all compatible types."
Item 46: Define non-member functions inside templates when type conversions are desired
Item 47: Use traits classes for information about types
Item 48: Be aware of template metaprogramming
Chapter 8. Customizing new and delete
Item 49: Understand the behavior of the new-handler
Item 50: Understand when it makes sense to replace new and delete
Item 51: Adhere to convention when writing new and delete
Item 52: Write placement delete if you write placement new
Chapter 9. Miscellany
Item 53: Pay attention to compiler warnings.
Item 54: Familiarize yourself with the standard library, including TR1
Item.55: Familiarize yourself with Boost.
Appendix A. Beyond Effective C++
Appendix B. Item Mappings Between Second and Third Editions
Index
index_SYMBOL
index_A
index_B
index_C
index_D
index_E
index_F
index_G
index_H
index_I
index_J
index_K
index_L
index_M
index_N
index_O
index_P
index_R
index_S
index_T
index_U
index_V
index_W
index_X
index_Z

Item.55: Familiarize yourself with Boost.

Searching for a collection of high-quality, open source, platform- and compiler-independent libraries? Look to Boost. Interested in joining a community of ambitious, talented C++ developers working on state-of-the-art library design and implementation? Look to Boost. Want a glimpse of what C++ might look like in the future? Look to Boost.

Boost is both a community of C++ developers and a collection of freely downloadable C++ libraries. Its web site is http://boost.org. You should bookmark it now.

There are many C++ organizations and web sites, of course, but Boost has two things going for it that no other organization can match. First, it has a uniquely close and influential relationship with the C++ standardization committee. Boost was founded by committee members, and there continues to be strong overlap between the Boost and committee memberships. In addition, Boost has always had as one of its goals to act as a testing ground for capabilities that could be added to Standard C++. One result of this relationship is that of the 14 new libraries introduced into C++ by TR1 (see Item 54), more than two-thirds are based on work done at Boost.

The second special characteristic of Boost is its process for accepting libraries. It's based on public peer review. If you'd like to contribute a library to Boost, you start by posting to the Boost developers mailing list to gauge interest in the library and initiate the process of preliminary examination of your work. Thus begins a cycle that the web site summarizes as "Discuss, refine, resubmit. Repeat until satisfied."

Eventually, you decide that your library is ready for formal submission. A review manager confirms that your library meets Boost's minimal requirements. For example, it must compile under at least two compilers (to demonstrate nominal portability), and you have to attest that the library can be made available under an acceptable license (e.g., the library must allow free commercial and non-commercial use). Then your submission is made available to the Boost community for official review. During the review period, volunteers go over your library materials (e.g., source code, design documents, user documentation, etc.) and consider questions such as these:

  • How good are the design and implementation?

  • Is the code portable across compilers and operating systems?

  • Is the library likely to be of use to its target audience, i.e., people working in the domain the library addresses?

  • Is the documentation clear, complete, and accurate?

These comments are posted to a Boost mailing list, so reviewers and others can see and respond to one another's remarks. At the end of the review period, the review manager decides whether your library is accepted, conditionally accepted, or rejected.

Peer reviews do a good job of keeping poorly written libraries out of Boost, but they also help educate library authors in the considerations that go into the design, implementation, and documentation of industrial-strength cross-platform libraries. Many libraries require more than one official review before being declared worthy of acceptance.

Boost contains dozens of libraries, and more are added on a continuing basis. From time to time, some libraries are also removed, typically because their functionality has been superseded by a newer library that offers greater functionality or a better design (e.g., one that is more flexible or more efficient).

The libraries vary widely in size and scope. At one extreme are libraries that conceptually require only a few lines of code (but are typically much longer after support for error handling and portability is added). One such library is Conversion, which provides safer or more convenient cast operators. Its numeric_cast function, for example, throws an exception if converting a numeric value from one type to another leads to overflow or underflow or a similar problem, and lexical_cast makes it possible to cast any type supporting operator<< into a string — very useful for diagnostics, logging, etc. At the other extreme are libraries offering such extensive capabilities, entire books have been written about them. These include the Boost Graph Library (for programming with arbitrary graph structures) and the Boost MPL Library ("metaprogramming library").

Boost's bevy of libraries addresses a cornucopia of topics, grouped into over a dozen general categories. Those categories include:

  • String and text processing, including libraries for type-safe printf-like formatting, regular expressions (the basis for similar functionality in TR1 — see Item 54), and tokenizing and parsing.

  • Containers, including libraries for fixed-size arrays with an STL-like interface (see Item 54), variable-sized bitsets, and multidimensional arrays.

  • Function objects and higher-order programming, including several libraries that were used as the basis for functionality in TR1. One interesting library is the Lambda library, which makes it so easy to create function objects on the fly, you're unlikely to realize that's what you're doing:

    
    using namespace boost::lambda;                    // make boost::lambda
    
                                                      // functionality visible
    
    
    
    std::vector<int> v;
    
    
    
    ...
    
    
    
    std::for_each(v.begin(), v.end(),                 // for each element x in
    
                  std::cout << _1 * 2 + 10 	<< "\n");  // v, print x*2+10;
    
                                                      // "_1" is the Lambda
    
                                                      // library's placeholder
    
                                                      // for the current element
    
    

  • Generic programming, including an extensive set of traits classes. (See Item 47 for information on traits).

  • Template metaprogramming (TMP — see Item 48), including a library for compile-time assertions, as well as the Boost MPL Library. Among the nifty things in MPL is support for STL-like data structures of compile-time entities like types, e.g.,

    
    // create a list-like compile-time container of three types (float,
    
    // double, and long double) and call the container "floats"
    
    typedef boost::mpl::list<float, double, long double> floats;
    
    
    
    // create a new compile-time list of types consisting of the types in
    
    // "floats" plus "int" inserted at the front; call the new container "types"
    
    typedef boost::mpl::push_front<floats, int>::type types;
    
    

    Such containers of types (often known as typelists, though they can also be based on an mpl::vector as well as an mpl::list) open the door to a wide range of powerful and important TMP applications.

  • Math and numerics, including libraries for rational numbers; octonions and quaternions; greatest common divisor and least common multiple computations; and random numbers (yet another library that influenced related functionality in TR1).

  • Correctness and testing, including libraries for formalizing implicit template interfaces (see Item 41) and for facilitating test-first programming.

  • Data structures, including libraries for type-safe unions (i.e., storing variant "any" types) and the tuple library that led to the corresponding TR1 functionality.

  • Inter-language support, including a library to allow seamless interoperability between C++ and Python.

  • Memory, including the Pool library for high-performance fixed-size allocators (see Item 50); and a variety of smart pointers (see Item 13), including (but not limited to) the smart pointers in TR1. One such non-TR1 smart pointer is scoped_array, an auto_ptr-like smart pointer for dynamically allocated arrays; Item 44 shows an example use.

  • Miscellaneous, including libraries for CRC checking, date and time manipulations, and traversing file systems.

Remember, that's just a sampling of the libraries you'll find at Boost. It's not an exhaustive list.

Boost offers libraries that do many things, but it doesn't cover the entire programming landscape. For example, there is no library for GUI development, nor is there one for communicating with databases. At least there's not now — not as I write this. By the time you read it, however, there might be. The only way to know for sure is to check. I suggest you do it right now: http://boost.org. Even if you don't find exactly what you're looking for, you're certain to find something interesting there.

Things to Remember

  • Boost is a community and web site for the development of free, open source, peer-reviewed C++ libraries. Boost plays an influential role in C++ standardization.

  • Boost offers implementations of many TR1 components, but it also offers many other libraries, too.