1 <chapter xmlns="http://docbook.org/ns/docbook" version="5.0"
2 xml:id="std.support" xreflabel="Support">
3 <?dbhtml filename="support.html"?>
7 <indexterm><primary>Support</primary></indexterm>
20 This part deals with the functions called and objects created
21 automatically during the course of a program's existence.
25 While we can't reproduce the contents of the Standard here (you
26 need to get your own copy from your nation's member body; see our
27 homepage for help), we can mention a couple of changes in what
28 kind of support a C++ program gets from the Standard Library.
31 <section xml:id="std.support.types" xreflabel="Types"><info><title>Types</title></info>
32 <?dbhtml filename="fundamental_types.html"?>
34 <section xml:id="std.support.types.fundamental" xreflabel="Fundamental Types"><info><title>Fundamental Types</title></info>
37 C++ has the following builtin types:
85 These fundamental types are always available, without having to
86 include a header file. These types are exactly the same in
91 Specializing parts of the library on these types is prohibited:
96 <section xml:id="std.support.types.numeric_limits" xreflabel="Numeric Properties"><info><title>Numeric Properties</title></info>
101 The header <filename class="headerfile">limits</filename> defines
102 traits classes to give access to various implementation
103 defined-aspects of the fundamental types. The traits classes --
104 fourteen in total -- are all specializations of the template class
105 <classname>numeric_limits</classname>, documented <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a00593.html">here</link>
106 and defined as follows:
110 template<typename T>
113 static const bool is_specialized;
114 static T max() throw();
115 static T min() throw();
117 static const int digits;
118 static const int digits10;
119 static const bool is_signed;
120 static const bool is_integer;
121 static const bool is_exact;
122 static const int radix;
123 static T epsilon() throw();
124 static T round_error() throw();
126 static const int min_exponent;
127 static const int min_exponent10;
128 static const int max_exponent;
129 static const int max_exponent10;
131 static const bool has_infinity;
132 static const bool has_quiet_NaN;
133 static const bool has_signaling_NaN;
134 static const float_denorm_style has_denorm;
135 static const bool has_denorm_loss;
136 static T infinity() throw();
137 static T quiet_NaN() throw();
138 static T denorm_min() throw();
140 static const bool is_iec559;
141 static const bool is_bounded;
142 static const bool is_modulo;
144 static const bool traps;
145 static const bool tinyness_before;
146 static const float_round_style round_style;
151 <section xml:id="std.support.types.null" xreflabel="NULL"><info><title>NULL</title></info>
154 The only change that might affect people is the type of
155 <constant>NULL</constant>: while it is required to be a macro,
156 the definition of that macro is <emphasis>not</emphasis> allowed
157 to be <constant>(void*)0</constant>, which is often used in C.
161 For <command>g++</command>, <constant>NULL</constant> is
162 <code>#define</code>'d to be
163 <constant>__null</constant>, a magic keyword extension of
164 <command>g++</command>.
168 The biggest problem of #defining <constant>NULL</constant> to be
169 something like <quote>0L</quote> is that the compiler will view
170 that as a long integer before it views it as a pointer, so
171 overloading won't do what you expect. (This is why
172 <command>g++</command> has a magic extension, so that
173 <constant>NULL</constant> is always a pointer.)
176 <para>In his book <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.awprofessional.com/titles/0-201-92488-9/"><emphasis>Effective
177 C++</emphasis></link>, Scott Meyers points out that the best way
178 to solve this problem is to not overload on pointer-vs-integer
179 types to begin with. He also offers a way to make your own magic
180 <constant>NULL</constant> that will match pointers before it
184 <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.awprofessional.com/titles/0-201-31015-5/">the
185 Effective C++ CD example</link>
191 <section xml:id="std.support.memory" xreflabel="Dynamic Memory"><info><title>Dynamic Memory</title></info>
192 <?dbhtml filename="dynamic_memory.html"?>
195 There are six flavors each of <function>new</function> and
196 <function>delete</function>, so make certain that you're using the right
197 ones. Here are quickie descriptions of <function>new</function>:
201 single object form, throwing a
202 <classname>bad_alloc</classname> on errors; this is what most
203 people are used to using
206 Single object "nothrow" form, returning NULL on errors
209 Array <function>new</function>, throwing
210 <classname>bad_alloc</classname> on errors
213 Array nothrow <function>new</function>, returning
214 <constant>NULL</constant> on errors
217 Placement <function>new</function>, which does nothing (like
221 Placement array <function>new</function>, which also does
226 They are distinguished by the parameters that you pass to them, like
227 any other overloaded function. The six flavors of <function>delete</function>
228 are distinguished the same way, but none of them are allowed to throw
229 an exception under any circumstances anyhow. (They match up for
233 Remember that it is perfectly okay to call <function>delete</function> on a
234 NULL pointer! Nothing happens, by definition. That is not the
235 same thing as deleting a pointer twice.
238 By default, if one of the <quote>throwing <function>new</function>s</quote> can't
239 allocate the memory requested, it tosses an instance of a
240 <classname>bad_alloc</classname> exception (or, technically, some class derived
241 from it). You can change this by writing your own function (called a
242 new-handler) and then registering it with <function>set_new_handler()</function>:
245 typedef void (*PFV)(void);
248 static PFV old_handler;
250 void my_new_handler ()
253 popup_window ("Dude, you are running low on heap memory. You
254 should, like, close some windows, or something.
255 The next time you run out, we're gonna burn!");
256 set_new_handler (old_handler);
262 safety = new char[500000];
263 old_handler = set_new_handler (&my_new_handler);
268 <classname>bad_alloc</classname> is derived from the base <classname>exception</classname>
269 class defined in Sect1 19.
273 <section xml:id="std.support.termination" xreflabel="Termination"><info><title>Termination</title></info>
274 <?dbhtml filename="termination.html"?>
276 <section xml:id="support.termination.handlers" xreflabel="Termination Handlers"><info><title>Termination Handlers</title></info>
279 Not many changes here to <filename class="headerfile">cstdlib</filename>. You should note that the
280 <function>abort()</function> function does not call the
281 destructors of automatic nor static objects, so if you're
282 depending on those to do cleanup, it isn't going to happen.
283 (The functions registered with <function>atexit()</function>
284 don't get called either, so you can forget about that
288 The good old <function>exit()</function> function can be a bit
289 funky, too, until you look closer. Basically, three points to
292 <orderedlist inheritnum="ignore" continuation="restarts">
295 Static objects are destroyed in reverse order of their creation.
300 Functions registered with <function>atexit()</function> are called in
301 reverse order of registration, once per registration call.
302 (This isn't actually new.)
307 The previous two actions are <quote>interleaved,</quote> that is,
308 given this pseudocode:
311 extern "C or C++" void f1 (void);
312 extern "C or C++" void f2 (void);
320 then at a call of <function>exit()</function>,
321 <varname>f2</varname> will be called, then
322 <varname>obj2</varname> will be destroyed, then
323 <varname>f1</varname> will be called, and finally
324 <varname>obj1</varname> will be destroyed. If
325 <varname>f1</varname> or <varname>f2</varname> allow an
326 exception to propagate out of them, Bad Things happen.
331 Note also that <function>atexit()</function> is only required to store 32
332 functions, and the compiler/library might already be using some of
333 those slots. If you think you may run out, we recommend using
334 the <function>xatexit</function>/<function>xexit</function> combination from <literal>libiberty</literal>, which has no such limit.
338 <section xml:id="support.termination.verbose" xreflabel="Verbose Terminate Handler"><info><title>Verbose Terminate Handler</title></info>
339 <?dbhtml filename="verbose_termination.html"?>
342 If you are having difficulty with uncaught exceptions and want a
343 little bit of help debugging the causes of the core dumps, you can
344 make use of a GNU extension, the verbose terminate handler.
348 #include <exception>
352 std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
355 throw <replaceable>anything</replaceable>;
360 The <function>__verbose_terminate_handler</function> function
361 obtains the name of the current exception, attempts to demangle
362 it, and prints it to stderr. If the exception is derived from
363 <classname>exception</classname> then the output from
364 <function>what()</function> will be included.
368 Any replacement termination function is required to kill the
369 program without returning; this one calls abort.
377 #include <exception>
378 #include <stdexcept>
380 struct argument_error : public std::runtime_error
382 argument_error(const std::string& s): std::runtime_error(s) { }
387 std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
389 throw argument_error(<quote>argc is greater than 5!</quote>);
396 With the verbose terminate handler active, this gives:
402 terminate called after throwing a `int'
404 % ./a.out f f f f f f f f f f f
405 terminate called after throwing an instance of `argument_error'
406 what(): argc is greater than 5!
412 The 'Aborted' line comes from the call to
413 <function>abort()</function>, of course.
417 This is the default termination handler; nothing need be done to
418 use it. To go back to the previous <quote>silent death</quote>
419 method, simply include <filename>exception</filename> and
420 <filename>cstdlib</filename>, and call
424 std::set_terminate(std::abort);
428 After this, all calls to <function>terminate</function> will use
429 <function>abort</function> as the terminate handler.
433 Note: the verbose terminate handler will attempt to write to
434 stderr. If your application closes stderr or redirects it to an
435 inappropriate location,
436 <function>__verbose_terminate_handler</function> will behave in
437 an unspecified manner.