† A second, unrelated use of dynamic_cast
is to find the beginning of the memory occupied by an object. We explore that capability in Item 27.
† Alas, it is not uncommon for compilers to fail to enforce this restriction. Before you write programs that rely on it, test your compilers to make sure they behave correctly. If you've ever wondered if it makes sense to have functions return const
objects, now you know: sometimes it does, and postfix increment and decrement are examples.
† A complete implementation of an almost-standard auto_ptr
appears on pages 291-294.
† Well, almost always. If the exception is not caught, the program will terminate. In that case, there is no guarantee that local objects (such as w
in the example) will have their destructors called. Some compilers call them, some do not. Both behaviors are valid.
† Provided, again, that the exception is caught.
† Now there is. In July 1995, the ISO/ANSI standardization committee for C++ added a function, uncaught_exception
, that returns true
if an exception is active and has not yet been caught.
† Compiler writers are actually allowed a slight bit of leeway regarding the "mandatory" nature of the copying; it can be eliminated under certain circumstances. Similar leeway provides the foundation for the return value optimization (see Item 20).
† Alas, it can't, at least not portably. Though many compilers accept the code shown on this page, the standardization committee has inexplicably decreed that "an exception specification shall not appear in a typedef." I don't know why. If you need a portable solution, you must — it hurts me to write this — make CallBackPtr
a macro, sigh.
† In July 1995, the ISO/ANSI committee standardizing C++ added a requirement that most STL iterators support the "->
" operator, so it->second
should now work. Some STL implementations fail to satisfy this requirement, however, so (*it).second
is still the more portable construct.
† In July 1996, the ISO/ANSI standardization committee declared that both named and unnamed objects may be optimized away via the return value optimization.
† At least that's what's supposed to happen. Alas, some compilers treat T(lhs)
as a cast to remove lhs
's const
ness, then add rhs
to lhs
and return a reference to the modified lhs
! Test your compilers before relying on the behavior described above.
† In July 1996, the ISO/ANSI standardization committee changed the default linkage of inline functions to external, so the problem I describe here has been eliminated, at least on paper. Your compilers may not yet be in accord with the standard, however, so your best bet is still to shy away from inline functions with static data.
† I have since become convinced that signature-based techniques are all but foolproof. For details, consult http://www.aristeia.com/BookErrata/M27Comments.html
.
† The string
type in the standard C++ library (see Item 35) uses a combination of solutions two and three. The reference returned from the non-const operator[]
is guaranteed to be valid until the next function call that might modify the string. After that, use of the reference (or the character to which it refers) yields undefined results. This allows the string's shareability flag to be reset to true
whenever a function is called that might modify the string.
† It turns out that it's not so predictable after all. The C++ standard doesn't specify the return value of type_info::name
, and different implementations do behave differently. A preferable design is to use a container-friendly class that wraps type_info
objects, such as Andrei Alexandrescu's TypeInfo
class, which is described in section 2.8 of his Modern C++ Design (Addison Wesley, 2001).
auto_ptr
Implementation† This is primarily because the specification for auto_ptr
has for years been a moving target. The final specification was adopted only in November 1997. For details, consult the auto_ptr
information at this book's WWW and FTP sites (see page 8). Note that the auto_ptr
described here omits a few details present in the official version, such as the fact that auto_ptr
is in the std
namespace (see Item 35) and that its member functions promise not to throw exceptions.