summaryrefslogtreecommitdiff
path: root/libstdc++-v3/doc/xml/manual/support.xml
blob: 2cb7205bd4452d008fdbdf3e9b85722a139fa560 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
<chapter xmlns="http://docbook.org/ns/docbook" version="5.0" 
	 xml:id="std.support" xreflabel="Support">
<?dbhtml filename="support.html"?>

<info><title>
  Support
  <indexterm><primary>Support</primary></indexterm>
</title>
  <keywordset>
    <keyword>
      ISO C++
    </keyword>
    <keyword>
      library
    </keyword>
  </keywordset>
</info>

  <para>
    This part deals with the functions called and objects created
    automatically during the course of a program's existence.
  </para>

  <para>
    While we can't reproduce the contents of the Standard here (you
    need to get your own copy from your nation's member body; see our
    homepage for help), we can mention a couple of changes in what
    kind of support a C++ program gets from the Standard Library.
  </para>

<section xml:id="std.support.types" xreflabel="Types"><info><title>Types</title></info>
  <?dbhtml filename="fundamental_types.html"?>
  
  <section xml:id="std.support.types.fundamental" xreflabel="Fundamental Types"><info><title>Fundamental Types</title></info>
    
    <para>
      C++ has the following builtin types:
    </para>
    <itemizedlist>
      <listitem><para>
	char
      </para></listitem>
      <listitem><para>
	signed char
      </para></listitem>
      <listitem><para>
	unsigned char
      </para></listitem>
      <listitem><para>
	signed short
      </para></listitem>
      <listitem><para>
	signed int
      </para></listitem>
      <listitem><para>
	signed long
      </para></listitem>
      <listitem><para>
	unsigned short
      </para></listitem>
      <listitem><para>
	unsigned int
      </para></listitem>
      <listitem><para>
	unsigned long
      </para></listitem>
      <listitem><para>
	bool
      </para></listitem>
      <listitem><para>
	wchar_t
      </para></listitem>
      <listitem><para>
	float
      </para></listitem>
      <listitem><para>
	double
      </para></listitem>
      <listitem><para>
	long double
      </para></listitem>
    </itemizedlist>

    <para>
      These fundamental types are always available, without having to
      include a header file. These types are exactly the same in
      either C++ or in C.
    </para>

    <para>
      Specializing parts of the library on these types is prohibited:
      instead, use a POD.
    </para>

  </section>
  <section xml:id="std.support.types.numeric_limits" xreflabel="Numeric Properties"><info><title>Numeric Properties</title></info>
    


    <para>
    The header <filename class="headerfile">limits</filename> defines
    traits classes to give access to various implementation
    defined-aspects of the fundamental types. The traits classes --
    fourteen in total -- are all specializations of the template class
    <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>
    and defined as follows:
    </para>

   <programlisting>
   template&lt;typename T&gt;
     struct class
     {
       static const bool is_specialized;
       static T max() throw();
       static T min() throw();

       static const int digits;
       static const int digits10;
       static const bool is_signed;
       static const bool is_integer;
       static const bool is_exact;
       static const int radix;
       static T epsilon() throw();
       static T round_error() throw();

       static const int min_exponent;
       static const int min_exponent10;
       static const int max_exponent;
       static const int max_exponent10;

       static const bool has_infinity;
       static const bool has_quiet_NaN;
       static const bool has_signaling_NaN;
       static const float_denorm_style has_denorm;
       static const bool has_denorm_loss;
       static T infinity() throw();
       static T quiet_NaN() throw();
       static T denorm_min() throw();

       static const bool is_iec559;
       static const bool is_bounded;
       static const bool is_modulo;

       static const bool traps;
       static const bool tinyness_before;
       static const float_round_style round_style;
     };
   </programlisting>
  </section>

  <section xml:id="std.support.types.null" xreflabel="NULL"><info><title>NULL</title></info>
    
    <para>
     The only change that might affect people is the type of
     <constant>NULL</constant>: while it is required to be a macro,
     the definition of that macro is <emphasis>not</emphasis> allowed
     to be <constant>(void*)0</constant>, which is often used in C.
    </para>

    <para>
     For <command>g++</command>, <constant>NULL</constant> is
     <code>#define</code>'d to be
     <constant>__null</constant>, a magic keyword extension of
     <command>g++</command>.
    </para>

    <para>
     The biggest problem of #defining <constant>NULL</constant> to be
     something like <quote>0L</quote> is that the compiler will view
     that as a long integer before it views it as a pointer, so
     overloading won't do what you expect. (This is why
     <command>g++</command> has a magic extension, so that
     <constant>NULL</constant> is always a pointer.)
    </para>

    <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
    C++</emphasis></link>, Scott Meyers points out that the best way
    to solve this problem is to not overload on pointer-vs-integer
    types to begin with.  He also offers a way to make your own magic
    <constant>NULL</constant> that will match pointers before it
    matches integers.
    </para>
    <para>See
      <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.awprofessional.com/titles/0-201-31015-5/">the
      Effective C++ CD example</link>
    </para>
  </section>

</section>

<section xml:id="std.support.memory" xreflabel="Dynamic Memory"><info><title>Dynamic Memory</title></info>
  <?dbhtml filename="dynamic_memory.html"?>
  
  <para>
    There are six flavors each of <function>new</function> and
    <function>delete</function>, so make certain that you're using the right
    ones. Here are quickie descriptions of <function>new</function>:
  </para>
  <itemizedlist>
      <listitem><para>
	single object form, throwing a
	<classname>bad_alloc</classname> on errors; this is what most
	people are used to using
      </para></listitem>
      <listitem><para>
	Single object "nothrow" form, returning NULL on errors
      </para></listitem>
      <listitem><para>
	Array <function>new</function>, throwing
	<classname>bad_alloc</classname> on errors
      </para></listitem>
      <listitem><para>
	Array nothrow <function>new</function>, returning
	<constant>NULL</constant> on errors
      </para></listitem>
      <listitem><para>
	Placement <function>new</function>, which does nothing (like
	it's supposed to)
      </para></listitem>
      <listitem><para>
	Placement array <function>new</function>, which also does
	nothing
      </para></listitem>
   </itemizedlist>
   <para>
     They are distinguished by the parameters that you pass to them, like
     any other overloaded function.  The six flavors of <function>delete</function>
     are distinguished the same way, but none of them are allowed to throw
     an exception under any circumstances anyhow.  (They match up for
     completeness' sake.)
   </para>
   <para>
     Remember that it is perfectly okay to call <function>delete</function> on a
     NULL pointer!  Nothing happens, by definition.  That is not the
     same thing as deleting a pointer twice.
   </para>
   <para>
     By default, if one of the <quote>throwing <function>new</function>s</quote> can't
     allocate the memory requested, it tosses an instance of a
     <classname>bad_alloc</classname> exception (or, technically, some class derived
     from it).  You can change this by writing your own function (called a
     new-handler) and then registering it with <function>set_new_handler()</function>:
   </para>
   <programlisting>
   typedef void (*PFV)(void);

   static char*  safety;
   static PFV    old_handler;

   void my_new_handler ()
   {
       delete[] safety;
       popup_window ("Dude, you are running low on heap memory.  You
		      should, like, close some windows, or something.
		      The next time you run out, we're gonna burn!");
       set_new_handler (old_handler);
       return;
   }

   int main ()
   {
       safety = new char[500000];
       old_handler = set_new_handler (&amp;my_new_handler);
       ...
   }
   </programlisting>
   <para>
     <classname>bad_alloc</classname> is derived from the base <classname>exception</classname>
     class defined in Sect1 19.
   </para>
</section>

<section xml:id="std.support.termination" xreflabel="Termination"><info><title>Termination</title></info>
  <?dbhtml filename="termination.html"?>
  
  <section xml:id="support.termination.handlers" xreflabel="Termination Handlers"><info><title>Termination Handlers</title></info>
    
    <para>
      Not many changes here to <filename class="headerfile">cstdlib</filename>.  You should note that the
      <function>abort()</function> function does not call the
      destructors of automatic nor static objects, so if you're
      depending on those to do cleanup, it isn't going to happen.
      (The functions registered with <function>atexit()</function>
      don't get called either, so you can forget about that
      possibility, too.)
    </para>
    <para>
      The good old <function>exit()</function> function can be a bit
      funky, too, until you look closer.  Basically, three points to
      remember are:
    </para>
    <orderedlist inheritnum="ignore" continuation="restarts">
      <listitem>
	<para>
	Static objects are destroyed in reverse order of their creation.
	</para>
      </listitem>
      <listitem>
	<para>
	Functions registered with <function>atexit()</function> are called in
	reverse order of registration, once per registration call.
	(This isn't actually new.)
	</para>
      </listitem>
      <listitem>
	<para>
	The previous two actions are <quote>interleaved,</quote> that is,
	given this pseudocode:
	</para>
<programlisting>
  extern "C or C++" void  f1 (void);
  extern "C or C++" void  f2 (void);

  static Thing obj1;
  atexit(f1);
  static Thing obj2;
  atexit(f2);
</programlisting>
	<para>
	then at a call of <function>exit()</function>,
	<varname>f2</varname> will be called, then
	<varname>obj2</varname> will be destroyed, then
	<varname>f1</varname> will be called, and finally
	<varname>obj1</varname> will be destroyed. If
	<varname>f1</varname> or <varname>f2</varname> allow an
	exception to propagate out of them, Bad Things happen.
	</para>
      </listitem>
    </orderedlist>
    <para>
      Note also that <function>atexit()</function> is only required to store 32
      functions, and the compiler/library might already be using some of
      those slots.  If you think you may run out, we recommend using
      the <function>xatexit</function>/<function>xexit</function> combination from <literal>libiberty</literal>, which has no such limit.
    </para>
  </section>

  <section xml:id="support.termination.verbose" xreflabel="Verbose Terminate Handler"><info><title>Verbose Terminate Handler</title></info>
  <?dbhtml filename="verbose_termination.html"?>
    
    <para>
      If you are having difficulty with uncaught exceptions and want a
      little bit of help debugging the causes of the core dumps, you can
      make use of a GNU extension, the verbose terminate handler.
    </para>

<programlisting>
#include &lt;exception&gt;

int main()
{
  std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
  ...

  throw <replaceable>anything</replaceable>;
}
</programlisting>

   <para>
     The <function>__verbose_terminate_handler</function> function
     obtains the name of the current exception, attempts to demangle
     it, and prints it to stderr.  If the exception is derived from
     <classname>exception</classname> then the output from
     <function>what()</function> will be included.
   </para>

   <para>
     Any replacement termination function is required to kill the
     program without returning; this one calls abort.
   </para>

   <para>
     For example:
   </para>

<programlisting>
#include &lt;exception&gt;
#include &lt;stdexcept&gt;

struct argument_error : public std::runtime_error
{
  argument_error(const std::string&amp; s): std::runtime_error(s) { }
};

int main(int argc)
{
  std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
  if (argc &gt; 5)
    throw argument_error(<quote>argc is greater than 5!</quote>);
  else
    throw argc;
}
</programlisting>

   <para>
     With the verbose terminate handler active, this gives:
   </para>

   <screen>
   <computeroutput>
   % ./a.out
   terminate called after throwing a `int'
   Aborted
   % ./a.out f f f f f f f f f f f
   terminate called after throwing an instance of `argument_error'
   what(): argc is greater than 5!
   Aborted
   </computeroutput>
   </screen>

   <para>
     The 'Aborted' line comes from the call to
     <function>abort()</function>, of course.
   </para>

   <para>
     This is the default termination handler; nothing need be done to
     use it.  To go back to the previous <quote>silent death</quote>
     method, simply include <filename>exception</filename> and
     <filename>cstdlib</filename>, and call
   </para>

   <programlisting>
     std::set_terminate(std::abort);
   </programlisting>

   <para>
     After this, all calls to <function>terminate</function> will use
     <function>abort</function> as the terminate handler.
   </para>

   <para>
     Note: the verbose terminate handler will attempt to write to
     stderr.  If your application closes stderr or redirects it to an
     inappropriate location,
     <function>__verbose_terminate_handler</function> will behave in
     an unspecified manner.
   </para>

  </section>
</section>

</chapter>