Chapter 19: Diagnostics

Chapter 19 deals with program diagnostics, such as exceptions and assertions. You know, all the things we wish weren't even necessary at all.


Adding data to exceptions

The standard exception classes carry with them a single string as data (usually describing what went wrong or where the 'throw' took place). It's good to remember that you can add your own data to these exceptions when extending the hierarchy:

   struct My_Exception : public std::runtime_error
       My_Exception (const string& whatarg)
           : std::runtime_error(whatarg), e(errno), id(GetDataBaseID()) { }
       int  errno_at_time_of_throw() const { return e; }
       DBID id_of_thing_that_threw() const { return id; }
       int    e;
       DBID   id;     // some user-defined type

Return to top of page or to the FAQ.

Exception class hierarchy diagram

At one point we were going to make up a PDF of the exceptions hierarchy, akin to the one done for the I/O class hierarchy. Time was our enemy. Since then we've moved to Doxygen, which has the useful property of not sucking. Specifically, when the source code is changed, the diagrams are automatically brought up to date. For the old way, we had to update the diagrams separately.

There are several links to the Doxygen-generated pages from here.

Return to top of page or to the FAQ.

Concept checkers -- new and improved!

Better taste! Less fat! Literally!

In 1999, SGI added concept checkers to their implementation of the STL: code which checked the template parameters of instantiated pieces of the STL, in order to insure that the parameters being used met the requirements of the standard. For example, the Standard requires that types passed as template parameters to vector be "Assignable" (which means what you think it means). The checking was done during compilation, and none of the code was executed at runtime.

Unfortunately, the size of the compiler files grew significantly as a result. The checking code itself was cumbersome. And bugs were found in it on more than one occasion.

The primary author of the checking code, Jeremy Siek, had already started work on a replacement implementation. The new code has been formally reviewed and accepted into the Boost libraries, and we are pleased to incorporate it into the GNU C++ library.

The new version imposes a much smaller space overhead on the generated object file. The checks are also cleaner and easier to read and understand.

They are off by default for all versions of GCC from 3.0 to 3.4 (the latest release at the time of writing). They can be enabled at configure time with --enable-concept-checks. You can enable them on a per-translation-unit basis with #define _GLIBCXX_CONCEPT_CHECKS for GCC 3.4 and higher (or with #define _GLIBCPP_CONCEPT_CHECKS for versions 3.1, 3.2 and 3.3).

Return to top of page or to the FAQ.

See license.html for copying conditions. Comments and suggestions are welcome, and may be sent to the libstdc++ mailing list.