OSDN Git Service

611954fc988723abf0a982ee79ad96fe3cc8cef6
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / doc / xml / manual / extensions.xml
1 <?xml version='1.0'?>
2 <!DOCTYPE part PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" 
3  "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" 
4 [ ]>
5
6 <part id="manual.ext" xreflabel="Extensions">
7 <?dbhtml filename="extensions.html"?>
8  
9 <partinfo>
10   <keywordset>
11     <keyword>
12       ISO C++
13     </keyword>
14     <keyword>
15       library
16     </keyword>
17   </keywordset>
18 </partinfo>
19
20 <title>
21   Extensions
22   <indexterm><primary>Extensions</primary></indexterm>
23 </title>
24
25 <preface>
26   <title></title>
27 <para>
28   Here we will make an attempt at describing the non-Standard extensions to
29   the library.  Some of these are from SGI's STL, some of these are GNU's,
30   and some just seemed to appear on the doorstep.
31 </para>
32 <para><emphasis>Before</emphasis> you leap in and use any of these
33 extensions, be aware of two things:
34 </para>
35 <orderedlist>
36    <listitem>
37      <para>
38      Non-Standard means exactly that.  
39      </para>
40      <para>
41        The behavior, and the very
42        existence, of these extensions may change with little or no
43        warning.  (Ideally, the really good ones will appear in the next
44        revision of C++.)  Also, other platforms, other compilers, other
45        versions of g++ or libstdc++ may not recognize these names, or
46        treat them differently, or... 
47      </para>
48    </listitem>
49    <listitem>
50      <para>
51        You should know how to <ulink url="XXX">access
52        these headers properly</ulink>. 
53      </para>
54    </listitem>
55 </orderedlist>
56 </preface>
57
58 <!-- Chapter 01 : Compile Time Checks -->
59 <chapter id="manual.ext.compile_checks" xreflabel="Compile Time Checks">
60 <?dbhtml filename="ext_compile_checks.html"?>
61   <title>Compile Time Checks</title>
62   <para>
63     Also known as concept checking.
64   </para>
65    <para>In 1999, SGI added <emphasis>concept checkers</emphasis> to their implementation
66       of the STL:  code which checked the template parameters of
67       instantiated pieces of the STL, in order to insure that the parameters
68       being used met the requirements of the standard.  For example,
69       the Standard requires that types passed as template parameters to
70       <code>vector</code> be <quote>Assignable</quote> (which means what you think
71       it means).  The checking was done during compilation, and none of
72       the code was executed at runtime.
73    </para>
74    <para>Unfortunately, the size of the compiler files grew significantly
75       as a result.  The checking code itself was cumbersome.  And bugs
76       were found in it on more than one occasion.
77    </para>
78    <para>The primary author of the checking code, Jeremy Siek, had already
79       started work on a replacement implementation.  The new code has been
80       formally reviewed and accepted into
81       <ulink url="http://www.boost.org/libs/concept_check/concept_check.htm">the
82       Boost libraries</ulink>, and we are pleased to incorporate it into the
83       GNU C++ library.
84    </para>
85    <para>The new version imposes a much smaller space overhead on the generated
86       object file.  The checks are also cleaner and easier to read and
87       understand.
88    </para>
89    <para>They are off by default for all versions of GCC from 3.0 to 3.4 (the
90       latest release at the time of writing).
91       They can be enabled at configure time with
92       <ulink url="../configopts.html"><literal>--enable-concept-checks</literal></ulink>.
93       You can enable them on a per-translation-unit basis with
94       <code>#define _GLIBCXX_CONCEPT_CHECKS</code> for GCC 3.4 and higher
95       (or with <code>#define _GLIBCPP_CONCEPT_CHECKS</code> for versions
96       3.1, 3.2 and 3.3).
97    </para>
98
99    <para>Please note that the upcoming C++ standard has first-class
100    support for template parameter constraints based on concepts in the core
101    language. This will obviate the need for the library-simulated concept
102    checking described above.
103    </para>
104
105 </chapter>
106
107 <!-- Chapter 02 : Debug Mode -->
108 <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" 
109             parse="xml" href="debug_mode.xml">
110 </xi:include>
111
112 <!-- Chapter 03 : Parallel Mode -->
113 <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" 
114             parse="xml" href="parallel_mode.xml">
115 </xi:include>
116
117 <!-- Chapter 04 : Allocators -->
118 <chapter id="manual.ext.allocator" xreflabel="Allocators">
119 <?dbhtml filename="ext_allocators.html"?>
120   <title>Allocators</title>
121
122   <!-- Section 01 : __mt_alloc -->
123   <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" 
124               parse="xml" href="mt_allocator.xml">
125   </xi:include>
126
127   <!-- Section 02 : bitmap_allocator -->
128   <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" 
129               parse="xml" href="bitmap_allocator.xml">
130   </xi:include>
131
132 </chapter>
133
134 <!-- Chapter 05 : Containers -->
135 <chapter id="manual.ext.containers" xreflabel="Containers">
136 <?dbhtml filename="ext_containers.html"?>
137   <title>Containers</title>
138   <para>
139   </para>
140   <sect1 id="manual.ext.containers.pbds" xreflabel="Policy Based Data Structures">
141     <title>Policy Based Data Structures</title>
142     <para>
143       <ulink
144       url="http://gcc.gnu.org/onlinedocs/libstdc++/ext/pb_ds/index.html">More details here</ulink>.
145     </para>
146   </sect1>
147
148   <sect1 id="manual.ext.containers.sgi" xreflabel="SGI ext">
149     <title>HP/SGI</title>
150     <para>
151     </para>
152
153 <para>A few extensions and nods to backwards-compatibility have been made with
154    containers.  Those dealing with older SGI-style allocators are dealt with
155    elsewhere.  The remaining ones all deal with bits:
156 </para>
157 <para>The old pre-standard <code>bit_vector</code> class is present for
158    backwards compatibility.  It is simply a typedef for the
159    <code>vector&lt;bool&gt;</code> specialization.
160 </para>
161 <para>The <code>bitset</code> class has a number of extensions, described in the
162    rest of this item.  First, we'll mention that this implementation of
163    <code>bitset&lt;N&gt;</code> is specialized for cases where N number of
164    bits will fit into a single word of storage.  If your choice of N is
165    within that range (&lt;=32 on i686-pc-linux-gnu, for example), then all
166    of the operations will be faster.
167 </para>
168 <para>There are
169    versions of single-bit test, set, reset, and flip member functions which
170    do no range-checking.  If we call them member functions of an instantiation
171    of &quot;bitset&lt;N&gt;,&quot; then their names and signatures are:
172 </para>
173    <programlisting>
174    bitset&lt;N&gt;&amp;   _Unchecked_set   (size_t pos);
175    bitset&lt;N&gt;&amp;   _Unchecked_set   (size_t pos, int val);
176    bitset&lt;N&gt;&amp;   _Unchecked_reset (size_t pos);
177    bitset&lt;N&gt;&amp;   _Unchecked_flip  (size_t pos);
178    bool         _Unchecked_test  (size_t pos);
179    </programlisting>
180    <para>Note that these may in fact be removed in the future, although we have
181    no present plans to do so (and there doesn't seem to be any immediate
182    reason to).
183 </para>
184 <para>The semantics of member function <code>operator[]</code> are not specified 
185    in the C++ standard.  A long-standing defect report calls for sensible
186    obvious semantics, which are already implemented here:  <code>op[]</code>
187    on a const bitset returns a bool, and for a non-const bitset returns a
188    <code>reference</code> (a nested type).  However, this implementation does
189    no range-checking on the index argument, which is in keeping with other
190    containers' <code>op[]</code> requirements.  The defect report's proposed
191    resolution calls for range-checking to be done.  We'll just wait and see...
192 </para>
193 <para>Finally, two additional searching functions have been added.  They return
194    the index of the first &quot;on&quot; bit, and the index of the first
195    &quot;on&quot; bit that is after <code>prev</code>, respectively:
196 </para>
197    <programlisting>
198    size_t _Find_first() const;
199    size_t _Find_next (size_t prev) const;</programlisting>
200 <para>The same caveat given for the _Unchecked_* functions applies here also.
201 </para>
202   </sect1>
203
204
205   <sect1 id="manual.ext.containers.deprecated_sgi" xreflabel="SGI ext dep">
206     <title>Deprecated HP/SGI</title>
207
208    <para>
209      The SGI hashing classes <classname>hash_set</classname> and
210      <classname>hash_set</classname> have been deprecated by the
211      unordered_set, unordered_multiset, unordered_map,
212      unordered_multimap containers in TR1 and the upcoming C++0x, and
213      may be removed in future releases.
214    </para>
215
216    <para>The SGI headers</para>
217    <programlisting>
218      &lt;hash_map&gt;
219      &lt;hash_set&gt;
220      &lt;rope&gt;
221      &lt;slist&gt;
222      &lt;rb_tree&gt;
223    </programlisting>
224    <para>are all here;
225       <code>&lt;hash_map&gt;</code> and <code>&lt;hash_set&gt;</code>
226       are deprecated but available as backwards-compatible extensions,
227       as discussed further below.  <code>&lt;rope&gt;</code> is the
228       SGI specialization for large strings (&quot;rope,&quot;
229       &quot;large strings,&quot; get it? Love that geeky humor.)
230       <code>&lt;slist&gt;</code> is a singly-linked list, for when the
231       doubly-linked <code>list&lt;&gt;</code> is too much space
232       overhead, and <code>&lt;rb_tree&gt;</code> exposes the red-black
233       tree classes used in the implementation of the standard maps and
234       sets.
235    </para>
236    <para>Each of the associative containers map, multimap, set, and multiset
237       have a counterpart which uses a
238       <ulink url="http://www.sgi.com/tech/stl/HashFunction.html">hashing
239       function</ulink> to do the arranging, instead of a strict weak ordering
240       function.  The classes take as one of their template parameters a
241       function object that will return the hash value; by default, an
242       instantiation of
243       <ulink url="http://www.sgi.com/tech/stl/hash.html">hash</ulink>.
244       You should specialize this functor for your class, or define your own,
245       before trying to use one of the hashing classes.
246    </para>
247    <para>The hashing classes support all the usual associative container
248       functions, as well as some extra constructors specifying the number
249       of buckets, etc.
250    </para>
251    <para>Why would you want to use a hashing class instead of the
252       <quote>normal</quote>implementations?  Matt Austern writes:
253    </para>
254    <blockquote>
255      <para>
256        <emphasis>[W]ith a well chosen hash function, hash tables
257        generally provide much better average-case performance than
258        binary search trees, and much worse worst-case performance.  So
259        if your implementation has hash_map, if you don't mind using
260        nonstandard components, and if you aren't scared about the
261        possibility of pathological cases, you'll probably get better
262        performance from hash_map.
263      </emphasis>
264      </para>
265    </blockquote>
266
267   </sect1>  
268 </chapter>
269
270 <!-- Chapter 06 : Utilities -->
271 <chapter id="manual.ext.util" xreflabel="Utilities">
272 <?dbhtml filename="ext_utilities.html"?>
273   <title>Utilities</title>
274   <para>
275     The &lt;functional&gt; header contains many additional functors
276     and helper functions, extending section 20.3.  They are
277     implemented in the file stl_function.h:
278   </para>
279   <itemizedlist>
280   <listitem>
281   <para><code>identity_element</code> for addition and multiplication. * 
282   </para>
283   </listitem>
284   <listitem>
285     <para>The functor <code>identity</code>, whose <code>operator()</code>
286       returns the argument unchanged. * 
287   </para>
288   </listitem>
289   <listitem>
290     <para>Composition functors <code>unary_function</code> and
291       <code>binary_function</code>, and their helpers <code>compose1</code>
292       and <code>compose2</code>. * 
293     </para>
294   </listitem>
295   <listitem>
296   <para><code>select1st</code> and <code>select2nd</code>, to strip pairs. * 
297   </para>
298   </listitem>
299   <listitem><para><code>project1st</code> and <code>project2nd</code>. * </para></listitem>
300   <listitem><para>A set of functors/functions which always return the same result.  They
301       are <code>constant_void_fun</code>, <code>constant_binary_fun</code>,
302       <code>constant_unary_fun</code>, <code>constant0</code>,
303       <code>constant1</code>, and <code>constant2</code>. * </para></listitem>
304   <listitem><para>The class <code>subtractive_rng</code>. * </para></listitem>
305   <listitem><para>mem_fun adaptor helpers <code>mem_fun1</code> and
306       <code>mem_fun1_ref</code> are provided for backwards compatibility. </para></listitem>
307 </itemizedlist>
308 <para>
309   20.4.1 can use several different allocators; they are described on the
310    main extensions page.
311 </para>
312 <para>
313   20.4.3 is extended with a special version of
314   <code>get_temporary_buffer</code> taking a second argument.  The
315   argument is a pointer, which is ignored, but can be used to specify
316   the template type (instead of using explicit function template
317   arguments like the standard version does).  That is, in addition to
318 </para>
319 <programlisting>
320 get_temporary_buffer&lt;int&gt;(5);
321 </programlisting>
322
323 <para>
324 you can also use
325 </para>
326
327 <programlisting>
328 get_temporary_buffer(5, (int*)0);
329 </programlisting>
330 <para>
331   A class <code>temporary_buffer</code> is given in stl_tempbuf.h. *
332 </para>
333 <para>
334   The specialized algorithms of section 20.4.4 are extended with
335   <code>uninitialized_copy_n</code>. *
336 </para>
337
338 </chapter>
339
340 <!-- Chapter 07 : Algorithms -->
341 <chapter id="manual.ext.algorithms" xreflabel="Algorithms">
342 <?dbhtml filename="ext_algorithms.html"?>
343   <title>Algorithms</title>
344 <para>25.1.6 (count, count_if) is extended with two more versions of count
345    and count_if.  The standard versions return their results.  The
346    additional signatures return void, but take a final parameter by
347    reference to which they assign their results, e.g.,
348 </para>
349    <programlisting>
350    void count (first, last, value, n);</programlisting>
351 <para>25.2 (mutating algorithms) is extended with two families of signatures,
352    random_sample and random_sample_n.
353 </para>
354 <para>25.2.1 (copy) is extended with
355 </para>
356    <programlisting>
357    copy_n (_InputIter first, _Size count, _OutputIter result);</programlisting>
358 <para>which copies the first 'count' elements at 'first' into 'result'.
359 </para>
360 <para>25.3 (sorting 'n' heaps 'n' stuff) is extended with some helper
361    predicates.  Look in the doxygen-generated pages for notes on these.
362 </para>
363    <itemizedlist>
364     <listitem><para><code>is_heap</code> tests whether or not a range is a heap.</para></listitem>
365     <listitem><para><code>is_sorted</code> tests whether or not a range is sorted in
366         nondescending order.</para></listitem>
367    </itemizedlist>
368 <para>25.3.8 (lexicographical_compare) is extended with
369 </para>
370    <programlisting>
371    lexicographical_compare_3way(_InputIter1 first1, _InputIter1 last1,
372                                  _InputIter2 first2, _InputIter2 last2)</programlisting>
373 <para>which does... what?
374 </para>
375
376 </chapter>
377
378 <!-- Chapter 08 : Numerics -->
379 <chapter id="manual.ext.numerics" xreflabel="Numerics">
380 <?dbhtml filename="ext_numerics.html"?>
381   <title>Numerics</title>
382 <para>26.4, the generalized numeric operations such as accumulate, are extended
383    with the following functions:
384 </para>
385    <programlisting>
386    power (x, n);
387    power (x, n, moniod_operation);</programlisting>
388 <para>Returns, in FORTRAN syntax, &quot;x ** n&quot; where n&gt;=0.  In the
389    case of n == 0, returns the <ulink url="#ch20">identity element</ulink> for the
390    monoid operation.  The two-argument signature uses multiplication (for
391    a true &quot;power&quot; implementation), but addition is supported as well.
392    The operation functor must be associative.
393 </para>
394 <para>The <code>iota</code> function wins the award for Extension With the
395    Coolest Name.  It &quot;assigns sequentially increasing values to a range.
396    That is, it assigns value to *first, value + 1 to *(first + 1) and so
397    on.&quot;  Quoted from SGI documentation.
398 </para>
399    <programlisting>
400    void iota(_ForwardIter first, _ForwardIter last, _Tp value);</programlisting>
401 </chapter>
402
403 <!-- Chapter 09 : Iterators -->
404 <chapter id="manual.ext.iterators" xreflabel="Iterators">
405 <?dbhtml filename="ext_iterators.html"?>
406   <title>Iterators</title>
407 <para>24.3.2 describes <code>struct iterator</code>, which didn't exist in the
408    original HP STL implementation (the language wasn't rich enough at the
409    time).  For backwards compatibility, base classes are provided which
410    declare the same nested typedefs:
411 </para>
412    <itemizedlist>
413     <listitem><para>input_iterator</para></listitem>
414     <listitem><para>output_iterator</para></listitem>
415     <listitem><para>forward_iterator</para></listitem>
416     <listitem><para>bidirectional_iterator</para></listitem>
417     <listitem><para>random_access_iterator</para></listitem>
418    </itemizedlist>
419 <para>24.3.4 describes iterator operation <code>distance</code>, which takes
420    two iterators and returns a result.  It is extended by another signature
421    which takes two iterators and a reference to a result.  The result is
422    modified, and the function returns nothing.
423 </para>
424
425 </chapter>
426
427 <!-- Chapter 08 : IO -->
428 <chapter id="manual.ext.io" xreflabel="IO">
429 <?dbhtml filename="ext_io.html"?>
430   <title>Input and Output</title>
431
432   <para>
433     Extensions allowing <code>filebuf</code>s to be constructed from
434     "C" types like  FILE*s and file descriptors.
435   </para>
436
437   <sect1 id="manual.ext.io.filebuf_derived" xreflabel="Derived filebufs">
438     <title>Derived filebufs</title>
439
440    <para>The v2 library included non-standard extensions to construct
441       <code>std::filebuf</code>s from C stdio types such as
442       <code>FILE*</code>s and POSIX file descriptors.
443       Today the recommended way to use stdio types with libstdc++
444       IOStreams is via the <code>stdio_filebuf</code> class (see below),
445       but earlier releases provided slightly different mechanisms.
446    </para>
447    <itemizedlist>
448      <listitem><para>3.0.x <code>filebuf</code>s have another ctor with this signature:
449         <code>basic_filebuf(__c_file_type*, ios_base::openmode, int_type);
450         </code>
451          This comes in very handy in a number of places, such as
452          attaching Unix sockets, pipes, and anything else which uses file
453          descriptors, into the IOStream buffering classes.  The three
454          arguments are as follows:
455          <itemizedlist>
456           <listitem><para><code>__c_file_type*      F   </code>
457               // the __c_file_type typedef usually boils down to stdio's FILE
458           </para></listitem>
459           <listitem><para><code>ios_base::openmode  M   </code>
460               // same as all the other uses of openmode
461           </para></listitem>
462           <listitem><para><code>int_type            B   </code>
463               // buffer size, defaults to BUFSIZ if not specified
464           </para></listitem>
465          </itemizedlist>
466          For those wanting to use file descriptors instead of FILE*'s, I
467          invite you to contemplate the mysteries of C's <code>fdopen()</code>.
468      </para></listitem>
469      <listitem><para>In library snapshot 3.0.95 and later, <code>filebuf</code>s bring
470          back an old extension:  the <code>fd()</code> member function.  The
471          integer returned from this function can be used for whatever file
472          descriptors can be used for on your platform.  Naturally, the
473          library cannot track what you do on your own with a file descriptor,
474          so if you perform any I/O directly, don't expect the library to be
475          aware of it.
476      </para></listitem>
477      <listitem><para>Beginning with 3.1, the extra <code>filebuf</code> constructor and
478          the <code>fd()</code> function were removed from the standard
479          filebuf.  Instead, <code>&lt;ext/stdio_filebuf.h&gt;</code> contains
480          a derived class called
481          <ulink url="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a00063.html"><code>__gnu_cxx::stdio_filebuf</code></ulink>.
482          This class can be constructed from a C <code>FILE*</code> or a file
483          descriptor, and provides the <code>fd()</code> function.
484      </para></listitem>
485    </itemizedlist>
486    <para>If you want to access a <code>filebuf</code>'s file descriptor to
487       implement file locking (e.g. using the <code>fcntl()</code> system
488       call) then you might be interested in Henry Suter's
489       <ulink url="http://suter.home.cern.ch/suter/RWLock.html">RWLock</ulink>
490       class.
491    </para>
492
493     <para>
494     </para>
495   </sect1>
496 </chapter>
497
498 <!-- Chapter 09 : Demangling -->
499 <chapter id="manual.ext.demangle" xreflabel="Demangling">
500 <?dbhtml filename="ext_demangling.html"?>
501   <title>Demangling</title>
502   <para>
503     Transforming C++ ABI identifiers (like RTTI symbols) into the
504     original C++ source identifiers is called
505     <quote>demangling.</quote>
506   </para>
507   <para>
508     If you have read the <ulink
509     url="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/namespaceabi.html">source
510     documentation for <code>namespace abi</code></ulink> then you are
511     aware of the cross-vendor C++ ABI in use by GCC.  One of the
512     exposed functions is used for demangling,
513     <code>abi::__cxa_demangle</code>.
514   </para>
515   <para>
516     In programs like <command>c++filt</command>, the linker, and other tools
517     have the ability to decode C++ ABI names, and now so can you.
518   </para>
519   <para>
520     (The function itself might use different demanglers, but that's the
521     whole point of abstract interfaces.  If we change the implementation,
522     you won't notice.)
523   </para>
524   <para>
525     Probably the only times you'll be interested in demangling at runtime
526     are when you're seeing <code>typeid</code> strings in RTTI, or when
527     you're handling the runtime-support exception classes.  For example:
528   </para>
529    <programlisting>
530 #include &lt;exception&gt;
531 #include &lt;iostream&gt;
532 #include &lt;cxxabi.h&gt;
533
534 struct empty { };
535
536 template &lt;typename T, int N&gt;
537   struct bar { };
538
539
540 int main()
541 {
542   int     status;
543   char   *realname;
544
545   // exception classes not in &lt;stdexcept&gt;, thrown by the implementation
546   // instead of the user
547   std::bad_exception  e;
548   realname = abi::__cxa_demangle(e.what(), 0, 0, &amp;status);
549   std::cout &lt;&lt; e.what() &lt;&lt; "\t=&gt; " &lt;&lt; realname &lt;&lt; "\t: " &lt;&lt; status &lt;&lt; '\n';
550   free(realname);
551
552
553   // typeid
554   bar&lt;empty,17&gt;          u;
555   const std::type_info  &amp;ti = typeid(u);
556
557   realname = abi::__cxa_demangle(ti.name(), 0, 0, &amp;status);
558   std::cout &lt;&lt; ti.name() &lt;&lt; "\t=&gt; " &lt;&lt; realname &lt;&lt; "\t: " &lt;&lt; status &lt;&lt; '\n';
559   free(realname);
560
561   return 0;
562 }
563    </programlisting>
564    <para>
565      This prints
566    </para>
567
568    <screen>
569    <computeroutput>
570       St13bad_exception       =&gt; std::bad_exception   : 0
571       3barI5emptyLi17EE       =&gt; bar&lt;empty, 17&gt;       : 0 
572    </computeroutput>
573    </screen>
574
575    <para>
576      The demangler interface is described in the source documentation
577      linked to above.  It is actually written in C, so you don't need to
578      be writing C++ in order to demangle C++.  (That also means we have to
579      use crummy memory management facilities, so don't forget to free()
580      the returned char array.)
581    </para>
582 </chapter>
583
584 <!-- Chapter 10 : Concurrency -->
585 <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" 
586             parse="xml" href="concurrency.xml">
587 </xi:include>
588
589 </part>