OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libstdc++-v3/] [doc/] [xml/] [manual/] [support.xml] - Blame information for rev 742

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 742 jeremybenn
2
         xml:id="std.support" xreflabel="Support">
3
4
 
5
</code></pre></td>
      </tr>
      <tr valign="middle">
         <td>6</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>  Support</code></pre></td>
      </tr>
      <tr valign="middle">
         <td>7</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>  <indexterm><primary>Support</primary></indexterm></code></pre></td>
      </tr>
      <tr valign="middle">
         <td>8</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>
9
  
10
    
11
      ISO C++
12
    
13
    
14
      library
15
    
16
  
17
18
 
19
  
20
    This part deals with the functions called and objects created
21
    automatically during the course of a program's existence.
22
  
23
 
24
  
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.
29
  
30
 
31
Types
32
  
33
 
34
  
Fundamental Types
35
 
36
    
37
      C++ has the following builtin types:
38
    
39
    
40
      
41
        char
42
      
43
      
44
        signed char
45
      
46
      
47
        unsigned char
48
      
49
      
50
        signed short
51
      
52
      
53
        signed int
54
      
55
      
56
        signed long
57
      
58
      
59
        unsigned short
60
      
61
      
62
        unsigned int
63
      
64
      
65
        unsigned long
66
      
67
      
68
        bool
69
      
70
      
71
        wchar_t
72
      
73
      
74
        float
75
      
76
      
77
        double
78
      
79
      
80
        long double
81
      
82
    
83
 
84
    
85
      These fundamental types are always available, without having to
86
      include a header file. These types are exactly the same in
87
      either C++ or in C.
88
    
89
 
90
    
91
      Specializing parts of the library on these types is prohibited:
92
      instead, use a POD.
93
    
94
 
95
  
96
  
Numeric Properties
97
 
98
 
99
 
100
    
101
    The header limits 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
    numeric_limits, documented here
106
    and defined as follows:
107
    
108
 
109
   
110
   template<typename T>
111
     struct class
112
     {
113
       static const bool is_specialized;
114
       static T max() throw();
115
       static T min() throw();
116
 
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();
125
 
126
       static const int min_exponent;
127
       static const int min_exponent10;
128
       static const int max_exponent;
129
       static const int max_exponent10;
130
 
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();
139
 
140
       static const bool is_iec559;
141
       static const bool is_bounded;
142
       static const bool is_modulo;
143
 
144
       static const bool traps;
145
       static const bool tinyness_before;
146
       static const float_round_style round_style;
147
     };
148
   
149
  
150
 
151
  
NULL
152
 
153
    
154
     The only change that might affect people is the type of
155
     NULL: while it is required to be a macro,
156
     the definition of that macro is not allowed
157
     to be (void*)0, which is often used in C.
158
    
159
 
160
    
161
     For g++, NULL is
162
     #define'd to be
163
     __null, a magic keyword extension of
164
     g++.
165
    
166
 
167
    
168
     The biggest problem of #defining NULL to be
169
     something like 0L 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
     g++ has a magic extension, so that
173
     NULL is always a pointer.)
174
    
175
 
176
    In his book Effective
177
    C++, 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
    NULL that will match pointers before it
181
    matches integers.
182
    
183
    See
184
      the
185
      Effective C++ CD example
186
    
187
  
188
 
189
190
 
191
Dynamic Memory
192
  
193
 
194
  
195
    There are six flavors each of new and
196
    delete, so make certain that you're using the right
197
    ones. Here are quickie descriptions of new:
198
  
199
  
200
      
201
        single object form, throwing a
202
        bad_alloc on errors; this is what most
203
        people are used to using
204
      
205
      
206
        Single object "nothrow" form, returning NULL on errors
207
      
208
      
209
        Array new, throwing
210
        bad_alloc on errors
211
      
212
      
213
        Array nothrow new, returning
214
        NULL on errors
215
      
216
      
217
        Placement new, which does nothing (like
218
        it's supposed to)
219
      
220
      
221
        Placement array new, which also does
222
        nothing
223
      
224
   
225
   
226
     They are distinguished by the parameters that you pass to them, like
227
     any other overloaded function.  The six flavors of delete
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
230
     completeness' sake.)
231
   
232
   
233
     Remember that it is perfectly okay to call delete on a
234
     NULL pointer!  Nothing happens, by definition.  That is not the
235
     same thing as deleting a pointer twice.
236
   
237
   
238
     By default, if one of the throwing news can't
239
     allocate the memory requested, it tosses an instance of a
240
     bad_alloc 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 set_new_handler():
243
   
244
   
245
   typedef void (*PFV)(void);
246
 
247
   static char*  safety;
248
   static PFV    old_handler;
249
 
250
   void my_new_handler ()
251
   {
252
       delete[] safety;
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);
257
       return;
258
   }
259
 
260
   int main ()
261
   {
262
       safety = new char[500000];
263
       old_handler = set_new_handler (&my_new_handler);
264
       ...
265
   }
266
   
267
   
268
     bad_alloc is derived from the base exception
269
     class defined in Sect1 19.
270
   
271
272
 
273
Termination
274
  
275
 
276
  
Termination Handlers
277
 
278
    
279
      Not many changes here to cstdlib.  You should note that the
280
      abort() 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 atexit()
284
      don't get called either, so you can forget about that
285
      possibility, too.)
286
    
287
    
288
      The good old exit() function can be a bit
289
      funky, too, until you look closer.  Basically, three points to
290
      remember are:
291
    
292
    
293
      
294
        
295
        Static objects are destroyed in reverse order of their creation.
296
        
297
      
298
      
299
        
300
        Functions registered with atexit() are called in
301
        reverse order of registration, once per registration call.
302
        (This isn't actually new.)
303
        
304
      
305
      
306
        
307
        The previous two actions are interleaved, that is,
308
        given this pseudocode:
309
        
310
311
  extern "C or C++" void  f1 (void);
312
  extern "C or C++" void  f2 (void);
313
 
314
  static Thing obj1;
315
  atexit(f1);
316
  static Thing obj2;
317
  atexit(f2);
318
319
        
320
        then at a call of exit(),
321
        f2 will be called, then
322
        obj2 will be destroyed, then
323
        f1 will be called, and finally
324
        obj1 will be destroyed. If
325
        f1 or f2 allow an
326
        exception to propagate out of them, Bad Things happen.
327
        
328
      
329
    
330
    
331
      Note also that atexit() 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 xatexit/xexit combination from libiberty, which has no such limit.
335
    
336
  
337
 
338
  
Verbose Terminate Handler
339
  
340
 
341
    
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.
345
    
346
 
347
348
#include <exception>
349
 
350
int main()
351
{
352
  std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
353
  ...
354
 
355
  throw anything;
356
}
357
358
 
359
   
360
     The __verbose_terminate_handler 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
     exception then the output from
364
     what() will be included.
365
   
366
 
367
   
368
     Any replacement termination function is required to kill the
369
     program without returning; this one calls abort.
370
   
371
 
372
   
373
     For example:
374
   
375
 
376
377
#include <exception>
378
#include <stdexcept>
379
 
380
struct argument_error : public std::runtime_error
381
{
382
  argument_error(const std::string& s): std::runtime_error(s) { }
383
};
384
 
385
int main(int argc)
386
{
387
  std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
388
  if (argc > 5)
389
    throw argument_error(argc is greater than 5!);
390
  else
391
    throw argc;
392
}
393
394
 
395
   
396
     With the verbose terminate handler active, this gives:
397
   
398
 
399
   
400
   
401
   % ./a.out
402
   terminate called after throwing a `int'
403
   Aborted
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!
407
   Aborted
408
   
409
   
410
 
411
   
412
     The 'Aborted' line comes from the call to
413
     abort(), of course.
414
   
415
 
416
   
417
     This is the default termination handler; nothing need be done to
418
     use it.  To go back to the previous silent death
419
     method, simply include exception and
420
     cstdlib, and call
421
   
422
 
423
   
424
     std::set_terminate(std::abort);
425
   
426
 
427
   
428
     After this, all calls to terminate will use
429
     abort as the terminate handler.
430
   
431
 
432
   
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
     __verbose_terminate_handler will behave in
437
     an unspecified manner.
438
   
439
 
440
  
441
442
 
443

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.