OSDN Git Service

2008-01-18 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / doc / html / 17_intro / howto.html
1 <?xml version="1.0" encoding="ISO-8859-1"?>
2 <!DOCTYPE html
3           PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
4           "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
6 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
7 <head>
8    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
9    <meta name="AUTHOR" content="pme@gcc.gnu.org (Phil Edwards), Benjamin Kosnik, Felix Natter" />
10    <meta name="KEYWORDS" content="HOWTO, libstdc++, gcc, g++, libg++, STL" />
11    <meta name="DESCRIPTION" content="HOWTO for libstdc++ chapter 17." />
12    <meta name="GENERATOR" content="vi and eight fingers" />
13    <title>libstdc++ HOWTO:  Chapter 17: Library Introduction</title>
14 <link rel="StyleSheet" href="../lib3styles.css" type="text/css" />
15 <link rel="Start" href="../documentation.html" type="text/html"
16  title="GNU C++ Standard Library" />
17 <link rel="Next" href="../18_support/howto.html" type="text/html"
18   title="Library Support" />
19 <link rel="Copyright" href="license.html" type="text/html" />
20 <link rel="Help" href="../faq/index.html" type="text/html" title="F.A.Q." />
21 </head>
22 <body>
23
24 <h1 class="centered"><a name="top">Chapter 17:  Library Introduction</a></h1>
25
26 <p>Chapter 17 is actually a list of definitions and descriptions used
27    in the following chapters of the Standard when describing the actual
28    library.  Here, we use &quot;Introduction&quot; as an introduction
29    to the <em>GNU implementation of</em> the ISO Standard C++ Library.
30 </p>
31
32
33 <!-- ####################################################### -->
34 <hr />
35 <h1>Contents</h1>
36 <ul>
37    <li><a href="#2.0">Header Files</a></li>
38    <li><a href="#3.0">Namespaces</a></li>
39    <li><a href="#6">Macros</a></li>
40    <li><a href="#5">Implementation specific behavior</a></li>
41    <li><a href="#7">Multithreading</a></li>
42 </ul>
43
44 <hr />
45
46 <!-- ####################################################### -->
47
48 <h2><a name="2.0">Header Files</a></h2>
49    <p>The C++ standard specifies the entire set of header files that must be
50       available to all hosted implementations.  Actually, the word
51       &quot;files&quot; is a misnomer, since the contents of the headers
52       don't necessarily have to be in any kind of external file.  The
53       only rule is that when one <code>#include</code>'s a header, the
54       contents of that header become
55       available, no matter how.
56    </p>
57
58 <p>That said, in practice files are used.</p>
59
60 <p> There are two main types of include files: header files related to
61 a specific version of the ISO C++ standard (called Standard Headers),
62 and all others (TR1, C++ ABI, and Extensions).</p>
63
64 <p>Two dialects of standard headers are supported, corresponding to
65 the 1998 standard as updated for 2003, and the draft of the upcoming
66 200x standard.
67 </p>
68
69 <p>C++98/03 include files. These are available in the default compilation mode, ie <code>-std=c++98</code> or <code>-std=gnu++98</code>. 
70 </p>
71
72 <center><table border="1"><caption>C++98 Library Headers</caption>
73 <tr><td>&lt;algorithm&gt;</td><td>&lt;iomanip&gt;</td><td>&lt;list&gt;</td><td>&lt;ostream&gt;</td><td>&lt;streambuf&gt;</td></tr>
74 <tr><td>&lt;bitset&gt;</td><td>&lt;ios&gt;</td><td>&lt;locale&gt;</td><td>&lt;queue&gt;</td><td>&lt;string&gt;</td></tr>
75 <tr><td>&lt;complex&gt;</td><td>&lt;iosfwd&gt;</td><td>&lt;map&gt;</td><td>&lt;set&gt;</td><td>&lt;typeinfo&gt;</td></tr>
76 <tr><td>&lt;deque&gt;</td><td>&lt;iostream&gt;</td><td>&lt;memory&gt;</td><td>&lt;sstream&gt;</td><td>&lt;utility&gt;</td></tr>
77 <tr><td>&lt;exception&gt;</td><td>&lt;istream&gt;</td><td>&lt;new&gt;</td><td>&lt;stack&gt;</td><td>&lt;valarray&gt;</td></tr>
78 <tr><td>&lt;fstream&gt;</td><td>&lt;iterator&gt;</td><td>&lt;numeric&gt;</td><td>&lt;stdexcept&gt;</td><td>&lt;vector&gt;</td></tr>
79 <tr><td>&lt;functional&gt;</td><td>&lt;limits&gt;</td></tr>
80 </table></center>
81
82 <p></p>
83
84 <center><table border="1"><caption>C++98 Headers for C Library Facilities</caption>
85 <tr><td>&lt;cassert&gt;</td><td>&lt;ciso646&gt;</td><td>&lt;csetjmp&gt;</td><td>&lt;cstdio&gt;</td><td>&lt;ctime&gt;</td></tr>
86 <tr><td>&lt;cctype&gt;</td><td>&lt;climits&gt;</td><td>&lt;csignal&gt;</td><td>&lt;cstdlib&gt;</td><td>&lt;cwchar&gt;</td></tr>
87 <tr><td>&lt;cerrno&gt;</td><td>&lt;clocale&gt;</td><td>&lt;cstdarg&gt;</td><td>&lt;cstring&gt;</td><td>&lt;cwctype&gt;</td></tr>
88 <tr><td>&lt;cfloat&gt;</td><td>&lt;cmath&gt;</td><td>&lt;cstddef&gt;</td></tr>
89 </table></center>
90
91 <p>C++0x include files. These are only available in C++0x compilation mode, ie <code>-std=c++0x</code> or <code>-std=gnu++0x</code>. 
92 </p>
93
94 <center><table border="1"><caption>C++0x Library Headers</caption>
95 <tr><td>&lt;algorithm&gt;</td><td>&lt;iomanip&gt;</td><td>&lt;locale&gt;</td><td>&lt;regex&gt;</td><td>&lt;tuple&gt;</td></tr>
96 <tr><td>&lt;array&gt;</td><td>&lt;ios&gt;</td><td>&lt;map&gt;</td><td>&lt;set&gt;</td><td>&lt;typeinfo&gt;</td></tr>
97 <tr><td>&lt;bitset&gt;</td><td>&lt;iosfwd&gt;</td><td>&lt;memory&gt;</td><td>&lt;sstream&gt;</td><td>&lt;type_traits&gt;</td></tr>
98 <tr><td>&lt;complex&gt;</td><td>&lt;iostream&gt;</td><td>&lt;new&gt;</td><td>&lt;stack&gt;</td><td>&lt;unordered_map&gt;</td></tr>
99 <tr><td>&lt;deque&gt;</td><td>&lt;istream&gt;</td><td>&lt;numeric&gt;</td><td>&lt;stdexcept&gt;</td><td>&lt;unordered_set&gt;</td></tr>
100 <tr><td>&lt;exception&gt;</td><td>&lt;iterator&gt;</td><td>&lt;ostream&gt;</td><td>&lt;streambuf&gt;</td><td>&lt;utility&gt;</td></tr>
101 <tr><td>&lt;fstream&gt;</td><td>&lt;limits&gt;</td><td>&lt;queue&gt;</td><td>&lt;string&gt;</td><td>&lt;valarray&gt;</td></tr>
102 <tr><td>&lt;functional&gt;</td><td>&lt;list&gt;</td><td>&lt;random&gt;</td><td>&lt;system_error&gt;</td><td>&lt;vector&gt;</td></tr>
103 </table></center>
104
105 <p></p>
106
107 <center><table border="1"><caption>C++0x Headers for C Library Facilities</caption>
108 <tr><td>&lt;cassert&gt;</td><td>&lt;cfloat&gt;</td><td>&lt;cmath&gt;</td><td>&lt;cstddef&gt;</td><td>&lt;ctgmath&gt;</td></tr>
109 <tr><td>&lt;ccomplex&gt;</td><td>&lt;cinttypes&gt;</td><td>&lt;csetjmp&gt;</td><td>&lt;cstdint&gt;</td><td>&lt;ctime&gt;</td></tr>
110 <tr><td>&lt;cctype&gt;</td><td>&lt;ciso646&gt;</td><td>&lt;csignal&gt;</td><td>&lt;cstdio&gt;</td><td>&lt;cuchar&gt;</td></tr>
111 <tr><td>&lt;cerrno&gt;</td><td>&lt;climits&gt;</td><td>&lt;cstdarg&gt;</td><td>&lt;cstdlib&gt;</td><td>&lt;cwchar&gt;</td></tr>
112 <tr><td>&lt;cfenv&gt;</td><td>&lt;clocale&gt;</td><td>&lt;cstdbool&gt;</td><td>&lt;cstring&gt;</td><td>&lt;cwctype&gt;</td></tr>
113 </table></center>
114
115
116 <p>In addition, TR1 includes as:
117 </p>
118
119 <center><table border="1"><caption>TR1 Library Headers</caption>
120 <tr><td>&lt;tr1/array&gt;</td><td>&lt;tr1/memory&gt;</td><td>&lt;tr1/regex&gt;</td><td>&lt;tr1/type_traits&gt;</td><td>&lt;tr1/unordered_set&gt;</td></tr>
121 <tr><td>&lt;tr1/complex&gt;</td><td>&lt;tr1/random&gt;</td><td>&lt;tr1/tuple&gt;</td><td>&lt;tr1/unordered_map&gt;</td><td>&lt;tr1/utility&gt;</td></tr>
122 <tr><td>&lt;tr1/functional&gt;</td></tr>
123 </table></center>
124
125 <p></p>
126
127 <center><table border="1"><caption>TR1 Headers for C Library Facilities</caption>
128 <tr><td>&lt;tr1/cmath&gt;</td><td>&lt;tr1/cfloat&gt;</td><td>&lt;tr1/cstdarg&gt;</td><td>&lt;tr1/cstdio&gt;</td><td>&lt;tr1/ctime&gt;</td></tr>
129 <tr><td>&lt;tr1/ccomplex&gt;</td><td>&lt;tr1/cinttypes&gt;</td><td>&lt;tr1/cstdbool&gt;</td><td>&lt;tr1/cstdlib&gt;</td><td>&lt;tr1/cwchar&gt;</td></tr>
130 <tr><td>&lt;tr1/cfenv&gt;</td><td>&lt;tr1/climits&gt;</td><td>&lt;tr1/cstdint&gt;</td><td>&lt;tr1/ctgmath&gt;</td><td>&lt;tr1/cwctype&gt;</td></tr>
131 </table></center>
132
133 <p>Also included are files for the C++ ABI interface:
134 </p>
135 <center><table border="1"><caption>C++ ABI Headers</caption>
136 <tr><td>&lt;cxxabi.h&gt;</td><td>&lt;cxxabi_forced.h&gt;</td></tr>
137 </table></center>
138
139 <p>And a large variety of extensions.
140 </p>
141
142 <center><table border="1"><caption>Extension Headers</caption>
143 <tr><td>&lt;ext/algorithm&gt;</td><td>&lt;ext/debug_allocator.h&gt;</td><td>&lt;ext/mt_allocator.h&gt;</td><td>&lt;ext/pod_char_traits.h&gt;</td><td>&lt;ext/stdio_sync_filebuf.h&gt;</td></tr>
144 <tr><td>&lt;ext/array_allocator.h&gt;</td><td>&lt;ext/enc_filebuf.h&gt;</td><td>&lt;ext/new_allocator.h&gt;</td><td>&lt;ext/pool_allocator.h&gt;</td><td>&lt;ext/throw_allocator.h&gt;</td></tr>
145 <tr><td>&lt;ext/atomicity.h&gt;</td><td>&lt;ext/functional&gt;</td><td>&lt;ext/numeric&gt;</td><td>&lt;ext/rb_tree&gt;</td><td>&lt;ext/typelist.h&gt;</td></tr>
146 <tr><td>&lt;ext/bitmap_allocator.h&gt;</td><td>&lt;ext/iterator&gt;</td><td>&lt;ext/numeric_traits.h&gt;</td><td>&lt;ext/rope&gt;</td><td>&lt;ext/type_traits.h&gt;</td></tr>
147 <tr><td>&lt;ext/codecvt_specializations.h&gt;</td><td>&lt;ext/malloc_allocator.h&gt;</td><td>&lt;ext/pb_ds/assoc_container.h&gt;</td><td>&lt;ext/slist&gt;</td><td>&lt;ext/vstring.h&gt;</td></tr>
148 <tr><td>&lt;ext/concurrence.h&gt;</td><td>&lt;ext/memory&gt;</td><td>&lt;ext/pb_ds/priority_queue.h&gt;</td><td>&lt;ext/stdio_filebuf.h&gt;</td></tr>
149 </table></center>
150
151 <p></p>
152
153 <center><table border="1"><caption>Extension Debug Headers</caption>
154 <tr><td>&lt;debug/bitset&gt;</td><td>&lt;debug/list&gt;</td><td>&lt;debug/set&gt;</td><td>&lt;debug/unordered_map&gt;</td><td>&lt;debug/vector&gt;</td></tr>
155 <tr><td>&lt;debug/deque&gt;</td><td>&lt;debug/map&gt;</td><td>&lt;debug/string&gt;</td><td>&lt;debug/unordered_set&gt;</td></tr>
156 </table></center>
157
158 <p></p>
159
160 <center><table border="1"><caption>Extension Parallel Headers</caption>
161 <tr><td>&lt;parallel/algorithm&gt;</td><td>&lt;parallel/numeric&gt;</td></tr>
162 </table></center>
163
164 <hr />
165 <h2><a name="2.1">Recipes for mixing headers</a></h2>
166
167 <p> A few simple rules.
168 </p>
169
170 <p>First, mixing different dialects of the standard headers is not
171 possible. It's an all-or-nothing affair. Thus, code like
172 </p>
173
174 <pre>
175 #include &lt;array&gt;
176 #include &lt;functional&gt;
177 </pre>
178
179 <p>Implies C++0x mode. To use the entities in &lt;array&gt;, the C++0x
180 compilation mode must be used, which implies the C++0x functionality
181 (and deprecations) in &lt;functional&gt; will be present.
182 </p>
183
184 <p>Second, the other headers can be included with either dialect of
185 the standard headers, although features and types specific to C++0x
186 are still only enabled when in C++0x compilation mode. So, to use
187 rvalue references with <code>__gnu_cxx::vstring</code>, or to use the
188 debug-mode versions of <code>std::unordered_map</code>, one must use
189 the <code>std=gnu++0x</code> compiler flag. (Or <code>std=c++0x</code>, of course.)
190 </p>
191
192 <p>A special case of the second rule is the mixing of TR1 and C++0x
193 facilities. It is possible (although not especially prudent) to
194 include both the TR1 version and the C++0x version of header in the
195 same translation unit:
196 </p>
197
198 <pre>
199 #include &lt;tr1/type_traits&gt;
200 #include &lt;type_traits&gt;
201 </pre>
202
203 <p> Several parts of C++0x diverge quite substantially from TR1 predecessors.
204 </p>
205
206
207 <hr />
208 <h2><a name="2.2">The C Headers and <code>namespace std</code></a></h2>
209 <p>
210         The standard specifies that if one includes the C-style header
211         (&lt;math.h&gt; in this case), the symbols will be available
212         in the global namespace and perhaps in
213         namespace <code>std::</code> (but this is no longer a firm
214         requirement.) One the other hand, including the C++-style
215         header (&lt;cmath&gt;) guarantees that the entities will be
216         found in namespace std and perhaps in the global namespace.
217       </p>
218
219 <p>
220 Usage of C++-style headers is recommended, as then
221 C-linkage names can be disambiguated by explicit qualification, such
222 as by <code>std::abort</code>. In addition, the C++-style headers can
223 use function overloading to provide a simpler interface to certain
224 families of C-functions. For instance in &lt;cmath&gt;, the
225 function <code>std::sin</code> has overloads for all the builtin
226 floating-point types. This means that <code>std::sin</code> can be
227 used uniformly, instead of a combination
228 of <code>std::sinf</code>, <code>std::sin</code>,
229 and <code>std::sinl</code>.
230 </p>
231
232 <hr />
233 <h2><a name="2.3">Precompiled Headers</a></h2>
234
235 <p>There are three base header files that are provided. They can be
236 used to precompile the standard headers and extensions into binary
237 files that may the be used to speed compiles that use these headers.
238 </p>
239
240
241 <ul>
242 <li>stdc++.h
243 <p>Includes all standard headers. Actual content varies depending on
244 language dialect.
245 </p>
246 </li>
247
248 <li>stdtr1c++.h
249 <p>Includes all of &lt;stdc++.h&gt;, and adds all the TR1 headers.
250 </p>
251 </li>
252
253 <li>extc++.h
254 <p>Includes all of &lt;stdtr1c++.h&gt;, and adds all the Extension headers.
255 </p></li>
256 </ul>
257
258 <p>How to construct a .gch file from one of these base header files.</p>
259
260 <p>First, find the include directory for the compiler. One way to do
261 this is:</p>
262
263 <pre>
264 g++ -v hello.cc
265
266 #include &lt;...&gt; search starts here:
267  /mnt/share/bld/H-x86-gcc.20071201/include/c++/4.3.0
268 ...
269 End of search list.
270 </pre>
271
272
273 <p>Then, create a precompiled header file with the same flags that
274 will be used to compile other projects.</p>
275
276 <pre>
277 g++ -Winvalid-pch -x c++-header -g -O2 -o ./stdc++.h.gch /mnt/share/bld/H-x86-gcc.20071201/include/c++/4.3.0/x86_64-unknown-linux-gnu/bits/stdc++.h
278 </pre>
279
280 <p>The resulting file will be quite large: the current size is around
281 thirty megabytes. </p>
282
283 <p>How to use the resulting file.</p>
284
285 <pre>
286 g++ -I. -include stdc++.h  -H -g -O2 hello.cc 
287 </pre>
288
289 <p>Verification that the PCH file is being used is easy:</p>
290
291 <pre>
292 g++ -Winvalid-pch -I. -include stdc++.h -H -g -O2 hello.cc -o test.exe
293 ! ./stdc++.h.gch
294 . /mnt/share/bld/H-x86-gcc.20071201/include/c++/4.3.0/iostream
295 . /mnt/share/bld/H-x86-gcc.20071201include/c++/4.3.0/string
296 </pre>
297
298 <p>The exclamation point to the left of the <code>stdc++.h.gch</code> listing means that the generated PCH file was used, and thus the </p>
299 <p></p>
300
301 <p> Detailed information about creating precompiled header files can be found in the GCC <a href="http://gcc.gnu.org/onlinedocs/gcc/Precompiled-Headers.html">documentation</a>.
302 </p>
303
304
305 <hr />
306 <h2><a name="3.0">Namespaces</a></h2>
307
308
309 <p> There are three main namespaces.
310 </p>
311
312 <ul>
313 <li>std
314 <p>The ISO C++ standards specify that "all library entities are defined
315 within namespace std." This includes namepaces nested
316 within <code>namespace std</code>, such as <code>namespace
317 std::tr1</code>.
318 </p>
319 </li>
320 <li>abi
321 <p>Specified by the C++ ABI. This ABI specifies a number of type and
322 function APIs supplemental to those required by the ISO C++ Standard,
323 but necessary for interoperability.
324 </p>
325 </li>
326
327 <li>__gnu_
328 <p>Indicating one of several GNU extensions. Choices
329 include <code>__gnu_cxx</code>, <code>__gnu_debug</code>, <code>__gnu_parallel</code>,
330 and <code>__gnu_pbds</code>.
331 </p></li>
332 </ul>
333
334 <p> A complete list of implementation namespaces (including namespace contents) is available in the generated source <a href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/namespaces.html">documentation</a>. 
335 </p>
336
337
338 <hr />
339 <h2><a name="3.1">Namespace <code>std::</code></a></h2>
340
341 <p>
342       One standard requirement is that the library components are defined
343       in <code>namespace std::</code>. Thus, in order to use these types or
344       functions, one must do one of two things:
345 </p>
346
347       <div class="itemizedlist"><ul type="disc"> <li><p>put a kind of
348 <span class="emphasis"><i>using-declaration</i></span> in your source
349 (either <code>using namespace std;</code> or i.e. <code>using
350 std::string;</code>) This approach works well for individual source files, but
351 should not be used in a global context, like header files.
352           </p></li> <li><p>use a <span class="emphasis"><i>fully
353 qualified name</i></span> for each library symbol
354 (i.e. <code>std::string</code>, <code>std::cout</code>) Always can be
355 used, and usually enhanced, by strategic use of typedefs. (In the
356 cases where the qualified verbiage becomes unwieldy.)
357           </p></li>
358 </ul></div>
359
360 <hr />
361 <h2><a name="3.2">Using namespace composition</a></h2>
362
363 <p>
364 Best practice in programming suggests sequestering new data or
365 functionality in a sanely-named, unique namespace whenever
366 possible. This is considered an advantage over dumping everything in
367 the global namespace, as then name look-up can be explicitly enabled or
368 disabled as above, symbols are consistently mangled without repetitive
369 naming prefixes or macros, etc.
370 </p>
371
372 <p>For instance, consider a project that defines most of its classes in <code>namespace gtk</code>. It is possible to
373         adapt <code>namespace gtk</code> to <code>namespace std</code> by using a C++-feature called
374         <span class="emphasis"><i>namespace composition</i></span>. This is what happens if
375         a <span class="emphasis"><i>using</i></span>-declaration is put into a
376         namespace-definition: the imported symbol(s) gets imported into the
377         currently active namespace(s). For example:
378 </p>
379 <pre class="programlisting">
380 namespace gtk 
381 {
382   using std::string;
383   using std::tr1::array;
384
385   class Window { ... };
386 }
387 </pre>
388 <p>
389         In this example, <code>std::string</code> gets imported into
390         <code>namespace gtk</code>.  The result is that use of
391         <code>std::string</code> inside namespace gtk can just use <code>string</code>, without the explicit qualification. 
392         As an added bonus, 
393         <code>std::string</code> does not get imported into
394         the global namespace.  Additionally, a more elaborate arrangement can be made for backwards compatibility and portability, whereby the
395         <code>using</code>-declarations can wrapped in macros that
396         are set based on autoconf-tests to either &quot;&quot; or i.e. <code>using
397           std::string;</code> (depending on whether the system has
398         libstdc++ in <code>std::</code> or not).  (ideas from
399         <tt>&lt;<a href="mailto:llewelly@dbritsch.dsl.xmission.com">llewelly@dbritsch.dsl.xmission.com</a>&gt;</tt>, Karl Nelson
400         <tt>&lt;<a href="mailto:kenelson@ece.ucdavis.edu">kenelson@ece.ucdavis.edu</a>&gt;</tt>)
401 </p>
402
403 <hr />
404 <h2><a name="6">Macros for libstdc++</a></h2>
405
406    <p>All pre-processor switches and configurations are all gathered
407       in the file <code>c++config.h</code>, which is generated during
408       the libstdc++ configuration and build process, and included by
409       files part of the public libstdc++ API. Most of these macros
410       should not be used by consumers of libstdc++, and are reserved
411       for internal implementation use. <strong>These macros cannot be
412       redefined</strong>. However, a select handful of these macro
413       control libstdc++ extensions and extra features, or provide
414       versioning information for the API, and are able to be used.
415    </p>
416
417    <p>All library macros begin with <code>_GLIBCXX_</code> (except for
418    versions 3.1.x to 3.3.x, which use <code>_GLIBCPP_</code>).
419    </p>
420
421    <p>Below is the macro which users may check for library version
422       information. </p>
423
424     <dl>
425     <dt><code>__GLIBCXX__</code></dt> <dd>The current version of
426     libstdc++ in compressed ISO date format, form of an unsigned
427     long. For details on the value of this particular macro for a
428     particular release, please consult this <a href="abi.html">
429     document</a>.</dd> </dl>
430
431    <p>Below are the macros which users may change with #define/#undef or
432       with -D/-U compiler flags.  The default state of the symbol is
433       listed.</p>
434
435    <p>&quot;Configurable&quot; (or &quot;Not configurable&quot;) means
436       that the symbol is initially chosen (or not) based on
437       --enable/--disable options at library build and configure time
438       (documented <a href="../configopts.html">here</a>), with the
439       various --enable/--disable choices being translated to
440       #define/#undef).
441    </p> 
442
443    <p> &quot;ABI&quot; means that changing from the default value may
444   mean changing the ABI of compiled code. In other words, these
445   choices control code which has already been compiled (i.e., in a
446   binary such as libstdc++.a/.so).  If you explicitly #define or
447   #undef these macros, the <em>headers</em> may see different code
448   paths, but the <em>libraries</em> which you link against will not.
449   Experimenting with different values with the expectation of
450   consistent linkage requires changing the config headers before
451   building/installing the library.
452    </p>   
453
454     <dl>
455     <dt><code>_GLIBCXX_DEPRECATED</code></dt>
456     <dd>Defined by default. Not configurable. ABI-changing. Turning this off
457         removes older ARM-style iostreams code, and other anachronisms
458         from the API.  This macro is dependent on the version of the
459         standard being tracked, and as a result may give different results for
460         <code>-std=c++98</code> and <code>-std=c++0x</code>. This may
461         be useful in updating old C++ code which no longer meet the
462         requirements of the language, or for checking current code
463         against new language standards.  </dd>
464
465     <dt><code>_GLIBCXX_FORCE_NEW</code></dt> <dd>Undefined by
466     default. When defined, memory allocation and allocators controlled
467     by libstdc++ call operator new/delete without caching and
468     pooling. Configurable via
469     <code>--enable-libstdcxx-allocator</code>. ABI-changing.
470     </dd>
471
472
473     <dt><code>_GLIBCXX_CONCEPT_CHECKS</code></dt> <dd>Undefined by
474     default.  Configurable via <code>--enable-concept-checks</code>.
475     When defined, performs compile-time checking on certain template
476     instantiations to detect violations of the requirements of the
477     standard.  This is described in more detail <a
478     href="../19_diagnostics/howto.html#3">here</a>.</dd>
479
480     <dt><code>_GLIBCXX_DEBUG</code></dt>
481     <dd>Undefined by default. When defined, compiles
482     user code using the <a href="../ext/debug.html#safe">libstdc++ debug
483     mode</a>.
484     </dd>
485     <dt><code>_GLIBCXX_DEBUG_PEDANTIC</code></dt>
486     <dd>Undefined by default. When defined while
487     compiling with the <a href="../ext/debug.html#safe">libstdc++ debug
488     mode</a>, makes the debug mode extremely picky by making the use
489     of libstdc++ extensions and libstdc++-specific behavior into
490     errors.
491     </dd>
492     <dt><code>_GLIBCXX_PARALLEL</code></dt>
493     <dd>Undefined by default. When defined, compiles
494     user code using the <a href="../ext/parallel_mode.html">libstdc++ parallel
495     mode</a>.
496     </dd>
497     </dl>
498
499 <hr />
500 <h2><a name="7">The Standard C++ library and multithreading</a></h2>
501    <p>This section discusses issues surrounding the proper compilation
502       of multithreaded applications which use the Standard C++
503       library.  This information is GCC-specific since the C++
504       standard does not address matters of multithreaded applications.
505       Unless explicitly prefaced, all information in this section is
506       relevant to the GCC 3.0 release and all later releases.
507    </p>
508    <p>Earlier GCC releases had a somewhat different approach to
509       threading configuration and proper compilation.  Before GCC 3.0,
510       configuration of the threading model was dictated by compiler
511       command-line options and macros (both of which were somewhat
512       thread-implementation and port-specific).  There were no
513       guarantees related to being able to link code compiled with one
514       set of options and macro setting with another set.  For GCC 3.0,
515       configuration of the threading model used with libraries and
516       user-code is performed when GCC is configured and built using
517       the --enable-threads and --disable-threads options.  The ABI is
518       stable for symbol name-mangling and limited functional
519       compatibility exists between code compiled under different
520       threading models.
521    </p>
522    <p>All normal disclaimers aside, multithreaded C++ application are
523       only supported when libstdc++ and all user code was built with
524       compilers which report (via <code> gcc/g++ -v </code>) the same thread
525       model and that model is not <em>single</em>.  As long as your
526       final application is actually single-threaded, then it should be
527       safe to mix user code built with a thread model of
528       <em>single</em> with a libstdc++ and other C++ libraries built
529       with another thread model useful on the platform.  Other mixes
530       may or may not work but are not considered supported.  (Thus, if
531       you distribute a shared C++ library in binary form only, it may
532       be best to compile it with a GCC configured with
533       --enable-threads for maximal interchangeability and usefulness
534       with a user population that may have built GCC with either
535       --enable-threads or --disable-threads.)
536    </p>
537    <p>When you link a multithreaded application, you will probably
538       need to add a library or flag to g++.  This is a very
539       non-standardized area of GCC across ports.  Some ports support a
540       special flag (the spelling isn't even standardized yet) to add
541       all required macros to a compilation (if any such flags are
542       required then you must provide the flag for all compilations not
543       just linking) and link-library additions and/or replacements at
544       link time.  The documentation is weak.  Here is a quick summary
545       to display how ad hoc this is: On Solaris, both -pthreads and
546       -threads (with subtly different meanings) are honored.  On OSF,
547       -pthread and -threads (with subtly different meanings) are
548       honored.  On Linux/i386, -pthread is honored.  On FreeBSD,
549       -pthread is honored.  Some other ports use other switches.
550       AFAIK, none of this is properly documented anywhere other than
551       in ``gcc -dumpspecs'' (look at lib and cpp entries).
552    </p>
553    <p>See <a href="../faq/index.html#5_6">FAQ</a> (general overview), <a
554       href="../23_containers/howto.html#3">23</a> (containers), and <a
555       href="../27_io/howto.html#9">27</a> (I/O) for more information.
556    </p>
557    <p>The libstdc++ library has been designed so that it can be used in
558       multithreaded applications (with libstdc++-v2 this was
559       only true of the STL parts.)  The first problem is
560       finding a <em>fast</em> method of implementation portable to all
561       platforms.  Due to historical reasons, some of the library is
562       written against per-CPU-architecture spinlocks and other parts
563       against the gthr.h abstraction layer which is provided by gcc.
564       A minor problem that pops up every so often is different
565       interpretations of what &quot;thread-safe&quot; means for a
566       library (not a general program).  We currently use the <a
567       href="http://www.sgi.com/tech/stl/thread_safety.html">same
568       definition that SGI</a> uses for their STL subset.  However, the
569       exception for read-only containers only applies to the STL
570       components. This definition is widely-used and something similar
571       will be used in the next version of the C++ standard library.
572    </p>
573    <p>Here is a small link farm to threads (no pun) in the mail archives
574       that discuss the threading problem.  Each link is to the first
575       relevant message in the thread; from there you can use
576       &quot;Thread Next&quot; to move down the thread.  This farm is in
577       latest-to-oldest order.
578    </p>
579       <ul>
580         <li>Our threading expert Loren gives a breakdown of
581         <a href="http://gcc.gnu.org/ml/libstdc++/2001-10/msg00024.html">the
582         six situations involving threads</a> for the 3.0 release series.</li>
583         <li><a href="http://gcc.gnu.org/ml/libstdc++/2001-05/msg00384.html">
584         This message</a> inspired a recent updating of issues with threading
585         and the SGI STL library.  It also contains some example
586         POSIX-multithreaded STL code.</li>
587       </ul>
588    <p> (A large selection of links to older messages has been removed; many
589       of the messages from 1999 were lost in a disk crash, and the few
590       people with access to the backup tapes have been too swamped with work
591       to restore them.  Many of the points have been superseded anyhow.)
592    </p>
593    <p>This section will be updated as new and interesting issues come
594       to light.
595    </p>
596    <p>Return <a href="#top">to top of page</a> or
597       <a href="../faq/index.html">to the FAQ</a>.
598    </p>
599
600 <hr />
601 <h2><a name="5">Behavior specific to libstdc++</a></h2>
602    <p>The ISO standard defines the following phrase:
603    </p>
604      <blockquote><dl>
605      <dt><code>[1.3.5] implementation-defined behavior</code></dt>
606      <dd>behavior, for a well-formed program construct and correct data, that
607          depends on the implementation <strong>and that each implementation
608          shall document</strong>.
609      </dd>
610      </dl></blockquote>
611    <p>We do so here, for the C++ library only.  Behavior of the compiler,
612       linker, runtime loader, and other elements of &quot;the
613       implementation&quot; are documented elsewhere.  Everything listed in
614       Annex B, Implementation Qualities, are also part of the compiler, not
615       the library.
616    </p>
617    <p>For each entry, we give the section number of the standard, when
618       applicable.  This list is probably incomplet and inkorrekt.
619    </p>
620    <p><strong>[1.9]/11 #3</strong> If <code>isatty(3)</code> is true, then
621       interactive stream support is implied.
622    </p>
623    <p><strong>[17.4.4.5]</strong> Non-reentrant functions are probably best
624       discussed in the various sections on multithreading (see above).
625    </p>
626    <!-- [17.4.4.8]/3 says any function that doesn't have an exception-spec
627         can throw whatever we want; see also its footnote.  Let's list those
628         in the sections where the function itself occurs.
629    -->
630    <p><strong>[18.1]/4</strong> The type of <code>NULL</code> is described
631       <a href="../18_support/howto.html#1">here</a>.
632    </p>
633    <p><strong>[18.3]/8</strong> Even though it's listed in the library
634       sections, libstdc++ has zero control over what the cleanup code hands
635       back to the runtime loader.  Talk to the compiler people.  :-)
636    </p>
637    <p><strong>[18.4.2.1]/5</strong> (bad_alloc),<br />
638       <strong>[18.5.2]/5</strong> (bad_cast),<br />
639       <strong>[18.5.3]/5</strong> (bad_typeid),<br />
640       <strong>[18.6.1]/8</strong> (exception),<br />
641       <strong>[18.6.2.1]/5</strong> (bad_exception):  The <code>what()</code>
642       member function of class <code>std::exception</code>, and these other
643       classes publicly derived from it, simply returns the name of the
644       class.  But they are the <em>mangled</em> names; you will need to call
645       <code>c++filt</code> and pass the names as command-line parameters to
646       demangle them, or call a
647       <a href="../18_support/howto.html#5">runtime demangler function</a>.
648       (The classes in <code>&lt;stdexcept&gt;</code> have constructors which
649       require an argument to use later for <code>what()</code> calls, so the
650       problem of <code>what()</code>'s value does not arise in most
651       user-defined exceptions.)
652    </p>
653    <p><strong>[18.5.1]/7</strong> The return value of
654       <code>std::type_info::name()</code> is the mangled type name (see the
655       previous entry for more).
656    </p>
657    <p><strong>[20.1.5]/5</strong> <em>&quot;Implementors are encouraged to
658       supply libraries that can accept allocators that encapsulate more
659       general memory models and that support non-equal instances.  In such
660       implementations, any requirements imposed on allocators by containers
661       beyond those requirements that appear in Table 32, and the semantics
662       of containers and algorithms when allocator instances compare
663       non-equal, are implementation-defined.&quot;</em>  As yet we don't
664       have any allocators which compare non-equal, so we can't describe how
665       they behave.
666    </p>
667    <p><strong>[21.1.3.1]/3,4</strong>,<br />
668       <strong>[21.1.3.2]/2</strong>,<br />
669       <strong>[23.*]'s foo::iterator</strong>,<br />
670       <strong>[27.*]'s foo::*_type</strong>,<br />
671       <strong>others...</strong>
672       Nope, these types are called implementation-defined because you
673       shouldn't be taking advantage of their underlying types.  Listing them
674       here would defeat the purpose.  :-)
675    </p>
676    <p><strong>[21.1.3.1]/5</strong> I don't really know about the mbstate_t
677       stuff... see the <a href="../22_locale/howto.html">chapter 22 notes</a>
678       for what does exist.
679    </p>
680    <p><strong>[22.*]</strong> Anything and everything we have on locale
681       implementation will be described
682       <a href="../22_locale/howto.html">over here</a>.
683    </p>
684    <p><strong>[26.2.8]/9</strong> I have no idea what
685       <code>complex&lt;T&gt;</code>'s pow(0,0) returns.
686    </p>
687    <p><strong>[27.4.2.4]/2</strong> Calling
688       <code>std::ios_base::sync_with_stdio</code> after I/O has already been
689       performed on the standard stream objects will
690       flush the buffers, and <!-- this line might go away -->
691       destroy and recreate the underlying buffer instances.  Whether or not
692       the previously-written I/O is destroyed in this process depends mostly
693       on the --enable-libio choice:  for stdio, if the written data is
694       already in the stdio buffer, the data may be completely safe!
695    </p>
696    <p><strong>[27.6.1.1.2]</strong>,<br />
697       <strong>[27.6.2.3]</strong> The I/O sentry ctor and dtor can perform
698       additional work than the minimum required.  We are not currently taking
699       advantage of this yet.
700    </p>
701    <p><strong>[27.7.1.3]/16</strong>,<br />
702       <strong>[27.8.1.4]/10</strong>
703       The effects of <code>pubsetbuf/setbuf</code> are described
704       <a href="../27_io/howto.html#2">in this chapter</a>.
705    </p>
706    <p><strong>[27.8.1.4]/16</strong> Calling <code>fstream::sync</code> when
707       a get area exists will... whatever <code>fflush()</code> does, I think.
708    </p>
709    <p>Return <a href="#top">to top of page</a> or
710       <a href="../faq/index.html">to the FAQ</a>.
711    </p>
712
713     <!--
714     <dt><code></code></dt>
715     <dd>
716     </dd>
717     -->
718    <p>Return <a href="#top">to top of page</a> or
719       <a href="../faq/index.html">to the FAQ</a>.
720    </p>
721
722
723
724 <!-- ####################################################### -->
725
726 <hr />
727 <p class="fineprint"><em>
728 See <a href="license.html">license.html</a> for copying conditions.
729 Comments and suggestions are welcome, and may be sent to
730 <a href="mailto:libstdc++@gcc.gnu.org">the libstdc++ mailing list</a>.
731 </em></p>
732
733
734 </body>
735 </html>
736
737