1 <chapter xmlns="http://docbook.org/ns/docbook" version="5.0"
2 xml:id="std.diagnostics" xreflabel="Diagnostics">
3 <?dbhtml filename="diagnostics.html"?>
7 <indexterm><primary>Diagnostics</primary></indexterm>
10 <keyword>ISO C++</keyword>
11 <keyword>library</keyword>
17 <section xml:id="std.diagnostics.exceptions" xreflabel="Exceptions"><info><title>Exceptions</title></info>
18 <?dbhtml filename="exceptions.html"?>
21 <section xml:id="std.diagnostics.exceptions.api"><info><title>API Reference</title></info>
24 All exception objects are defined in one of the standard header
25 files: <filename>exception</filename>,
26 <filename>stdexcept</filename>, <filename>new</filename>, and
27 <filename>typeinfo</filename>.
31 The base exception object is <classname>exception</classname>,
32 located in <filename>exception</filename>. This object has no
33 <classname>string</classname> member.
37 Derived from this are several classes that may have a
38 <classname>string</classname> member: a full hierarchy can be
39 found in the source documentation.
46 <!-- Doxygen XML: api/group__exceptions.xml -->
49 <section xml:id="std.diagnostics.exceptions.data" xreflabel="Adding Data to Exceptions"><info><title>Adding Data to <classname>exception</classname></title></info>
52 The standard exception classes carry with them a single string as
53 data (usually describing what went wrong or where the 'throw' took
54 place). It's good to remember that you can add your own data to
55 these exceptions when extending the hierarchy:
58 struct My_Exception : public std::runtime_error
61 My_Exception (const string& whatarg)
62 : std::runtime_error(whatarg), e(errno), id(GetDataBaseID()) { }
63 int errno_at_time_of_throw() const { return e; }
64 DBID id_of_thing_that_threw() const { return id; }
67 DBID id; // some user-defined type
74 <section xml:id="std.diagnostics.errno" xreflabel="errno"><info><title>Use of errno by the library</title></info>
75 <?dbhtml filename="errno.html"?>
78 The C and POSIX standards guarantee that <varname>errno</varname>
79 is never set to zero by any library function.
80 The C++ standard has less to say about when <varname>errno</varname>
81 is or isn't set, but libstdc++ follows the same rule and never sets
86 On the other hand, there are few guarantees about when the C++ library
87 sets <varname>errno</varname> on error, beyond what is specified for
88 functions that come from the C library.
89 For example, when <function>std::stoi</function> throws an exception of
90 type <classname>std::out_of_range</classname>, <varname>errno</varname>
91 may or may not have been set to <constant>ERANGE</constant>.
95 Parts of the C++ library may be implemented in terms of C library
96 functions, which may result in <varname>errno</varname> being set
97 with no explicit call to a C function. For example, on a target where
98 <function>operator new</function> uses <function>malloc</function>
99 a failed memory allocation with <function>operator new</function> might
100 set <varname>errno</varname> to <constant>ENOMEM</constant>.
101 Which C++ library functions can set <varname>errno</varname> in this way
102 is unspecified because it may vary between platforms and between releases.
107 <section xml:id="std.diagnostics.concept_checking" xreflabel="Concept Checking"><info><title>Concept Checking</title></info>
108 <?dbhtml filename="concept_checking.html"?>
111 In 1999, SGI added <quote>concept checkers</quote> to their
112 implementation of the STL: code which checked the template
113 parameters of instantiated pieces of the STL, in order to insure
114 that the parameters being used met the requirements of the
115 standard. For example, the Standard requires that types passed as
116 template parameters to <classname>vector</classname> be
117 "Assignable" (which means what you think it means). The
118 checking was done during compilation, and none of the code was
122 Unfortunately, the size of the compiler files grew significantly
123 as a result. The checking code itself was cumbersome. And bugs
124 were found in it on more than one occasion.
127 The primary author of the checking code, Jeremy Siek, had already
128 started work on a replacement implementation. The new code was
129 formally reviewed and accepted into
130 <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.boost.org/libs/concept_check/concept_check.htm">the
131 Boost libraries</link>, and we are pleased to incorporate it into the
135 The new version imposes a much smaller space overhead on the generated
136 object file. The checks are also cleaner and easier to read and
141 They are off by default for all versions of GCC.
142 They can be enabled at configure time with
143 <link linkend="manual.intro.setup.configure"><literal>--enable-concept-checks</literal></link>.
144 You can enable them on a per-translation-unit basis with
145 <literal>-D_GLIBCXX_CONCEPT_CHECKS</literal>.
149 Please note that the checks are based on the requirements in the original
150 C++ standard, many of which were relaxed in the C++11 standard and so valid
151 C++11 code may be incorrectly rejected by the concept checks. Additionally,
152 some correct C++03 code might be rejected by the concept checks,
153 for example template argument types may need to be complete when used in
154 a template definition, rather than at the point of instantiation.
155 There are no plans to address these shortcomings.