OSDN Git Service

* doc/xml/manual/bitmap_allocator.xml: Fix typos.
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / doc / xml / manual / backwards_compatibility.xml
1 <section xmlns="http://docbook.org/ns/docbook" version="5.0" 
2          xml:id="manual.appendix.porting.backwards" xreflabel="backwards">
3 <?dbhtml filename="backwards.html"?>
4
5 <info><title>Backwards Compatibility</title>
6   <keywordset>
7     <keyword>
8       ISO C++
9     </keyword>
10     <keyword>
11       backwards
12     </keyword>
13   </keywordset>
14 </info>
15
16
17
18 <section xml:id="backwards.first"><info><title>First</title></info>
19
20
21 <para>The first generation GNU C++ library was called libg++.  It was a
22 separate GNU project, although reliably paired with GCC. Rumors imply
23 that it had a working relationship with at least two kinds of
24 dinosaur.
25 </para>
26
27 <para>Some background: libg++ was designed and created when there was no
28 ISO standard to provide guidance.  Classes like linked lists are now
29 provided for by <classname>list&lt;T&gt;</classname> and do not need to be
30 created by <function>genclass</function>.  (For that matter, templates exist
31 now and are well-supported, whereas genclass (mostly) predates them.)
32 </para>
33
34 <para>There are other classes in libg++ that are not specified in the
35 ISO Standard (e.g., statistical analysis).  While there are a lot of
36 really useful things that are used by a lot of people, the Standards
37 Committee couldn't include everything, and so a lot of those
38 <quote>obvious</quote> classes didn't get included.
39 </para>
40
41 <para>Known Issues include many of the limitations of its immediate ancestor.</para>
42
43 <para>Portability notes and known implementation limitations are as follows.</para>
44
45 <section><info><title>No <code>ios_base</code></title></info>
46   
47
48 <para> At least some older implementations don't have <code>std::ios_base</code>, so you should use <code>std::ios::badbit</code>, <code>std::ios::failbit</code> and <code>std::ios::eofbit</code> and <code>std::ios::goodbit</code>.
49 </para>
50 </section>
51
52 <section><info><title>No <code>cout</code> in <code>ostream.h</code>, no <code>cin</code> in <code>istream.h</code></title></info>
53
54
55 <para>
56         In earlier versions of the standard,
57         <filename class="headerfile">fstream.h</filename>,
58         <filename class="headerfile">ostream.h</filename>
59         and <filename class="headerfile">istream.h</filename>
60         used to define
61         <code>cout</code>, <code>cin</code> and so on. ISO C++ specifies that one needs to include
62         <filename class="headerfile">iostream</filename>
63         explicitly to get the required definitions.
64  </para>
65 <para> Some include adjustment may be required.</para>
66
67 <para>This project is no longer maintained or supported, and the sources
68 archived. For the desperate,
69 the <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://gcc.gnu.org/extensions.html">GCC extensions
70 page</link> describes where to find the last libg++ source. The code is
71 considered replaced and rewritten.
72 </para>
73 </section>
74 </section>
75
76 <section xml:id="backwards.second"><info><title>Second</title></info>
77
78
79 <para>
80   The second generation GNU C++ library was called libstdc++, or
81   libstdc++-v2. It spans the time between libg++ and pre-ISO C++
82   standardization and is usually associated with the following GCC
83   releases: egcs 1.x, gcc 2.95, and gcc 2.96.
84 </para>
85
86 <para>
87   The STL portions of this library are based on SGI/HP STL release 3.11.
88 </para>
89
90 <para>
91   This project is no longer maintained or supported, and the sources
92   archived.  The code is considered replaced and rewritten.
93 </para>
94
95 <para>
96   Portability notes and known implementation limitations are as follows.
97 </para>
98
99 <section><info><title>Namespace <code>std::</code> not supported</title></info>
100   
101
102   <para>
103     Some care is required to support C++ compiler and or library
104     implementation that do not have the standard library in
105     <code>namespace std</code>.
106   </para>
107
108   <para>
109     The following sections list some possible solutions to support compilers
110     that cannot ignore <code>std::</code>-qualified names.
111   </para>
112
113   <para>
114     First, see if the compiler has a flag for this. Namespace
115     back-portability-issues are generally not a problem for g++
116     compilers that do not have libstdc++ in <code>std::</code>, as the
117     compilers use <code>-fno-honor-std</code> (ignore
118     <code>std::</code>, <code>:: = std::</code>) by default. That is,
119     the responsibility for enabling or disabling <code>std::</code> is
120     on the user; the maintainer does not have to care about it. This
121     probably applies to some other compilers as well.
122   </para>
123
124   <para>
125     Second, experiment with a variety of pre-processor tricks.
126   </para>
127
128   <para>
129     By defining <code>std</code> as a macro, fully-qualified namespace
130     calls become global. Volia.
131   </para>
132
133 <programlisting>
134 #ifdef WICKEDLY_OLD_COMPILER
135 # define std
136 #endif
137 </programlisting>
138
139   <para>
140     Thanks to Juergen Heinzl who posted this solution on gnu.gcc.help.
141   </para>
142
143   <para>
144     Another pre-processor based approach is to define a macro
145     <code>NAMESPACE_STD</code>, which is defined to either
146     <quote> </quote> or <quote>std</quote> based on a compile-type
147     test. On GNU systems, this can be done with autotools by means of
148     an autoconf test (see below) for <code>HAVE_NAMESPACE_STD</code>,
149     then using that to set a value for the <code>NAMESPACE_STD</code>
150     macro.  At that point, one is able to use
151     <code>NAMESPACE_STD::string</code>, which will evaluate to
152     <code>std::string</code> or <code>::string</code> (i.e., in the
153     global namespace on systems that do not put <code>string</code> in
154     <code>std::</code>).
155   </para>
156
157 <programlisting>
158 dnl @synopsis AC_CXX_NAMESPACE_STD
159 dnl
160 dnl If the compiler supports namespace std, define
161 dnl HAVE_NAMESPACE_STD.
162 dnl
163 dnl @category Cxx
164 dnl @author Todd Veldhuizen
165 dnl @author Luc Maisonobe &lt;luc@spaceroots.org&gt;
166 dnl @version 2004-02-04
167 dnl @license AllPermissive
168 AC_DEFUN([AC_CXX_NAMESPACE_STD], [
169   AC_CACHE_CHECK(if g++ supports namespace std,
170   ac_cv_cxx_have_std_namespace,
171   [AC_LANG_SAVE
172   AC_LANG_CPLUSPLUS
173   AC_TRY_COMPILE([#include &lt;iostream&gt;
174                   std::istream&amp; is = std::cin;],,
175   ac_cv_cxx_have_std_namespace=yes, ac_cv_cxx_have_std_namespace=no)
176   AC_LANG_RESTORE
177   ])
178   if test "$ac_cv_cxx_have_std_namespace" = yes; then
179     AC_DEFINE(HAVE_NAMESPACE_STD,,[Define if g++ supports namespace std. ])
180   fi
181 ])
182 </programlisting>
183 </section>
184
185 <section><info><title>Illegal iterator usage</title></info>
186
187 <para>
188   The following illustrate implementation-allowed illegal iterator
189   use, and then correct use.
190 </para>
191
192 <itemizedlist>
193   <listitem>
194     <para>
195       you cannot do <code>ostream::operator&lt;&lt;(iterator)</code>
196       to print the address of the iterator =&gt; use
197       <code>operator&lt;&lt; &amp;*iterator</code> instead
198     </para>
199   </listitem>
200   <listitem>
201     <para>
202       you cannot clear an iterator's reference (<code>iterator =
203       0</code>) =&gt; use <code>iterator = iterator_type();</code>
204     </para>
205   </listitem>
206   <listitem>
207     <para>
208       <code>if (iterator)</code> won't work any more =&gt; use
209       <code>if (iterator != iterator_type())</code>
210     </para>
211   </listitem>
212 </itemizedlist>
213 </section>
214
215 <section><info><title><code>isspace</code> from <filename class="headerfile">cctype</filename> is a macro
216   </title></info>
217   
218
219   <para>
220     Glibc 2.0.x and 2.1.x define <filename class="headerfile">ctype.h</filename> functionality as macros
221     (isspace, isalpha etc.).
222   </para>
223
224   <para>
225     This implementations of libstdc++, however, keep these functions
226     as macros, and so it is not back-portable to use fully qualified
227     names. For example:
228   </para>
229
230 <programlisting>
231 #include &lt;cctype&gt;
232 int main() { std::isspace('X'); }
233 </programlisting>
234
235 <para>
236   Results in something like this:
237 </para>
238
239 <programlisting>
240 std:: (__ctype_b[(int) ( ( 'X' ) )] &amp; (unsigned short int) _ISspace ) ;
241 </programlisting>
242
243 <para>
244   A solution is to modify a header-file so that the compiler tells
245   <filename class="headerfile">ctype.h</filename> to define functions
246   instead of macros:
247 </para>
248
249 <programlisting>
250 // This keeps isalnum, et al from being propagated as macros.
251 #if __linux__
252 # define __NO_CTYPE 1
253 #endif
254 </programlisting>
255
256 <para>
257   Then, include <filename class="headerfile">ctype.h</filename>
258 </para>
259
260 <para>
261   Another problem arises if you put a <code>using namespace
262   std;</code> declaration at the top, and include <filename class="headerfile">ctype.h</filename>. This will result in
263   ambiguities between the definitions in the global namespace
264   (<filename class="headerfile">ctype.h</filename>) and the
265   definitions in namespace <code>std::</code>
266   (<code>&lt;cctype&gt;</code>).
267 </para>
268 </section>
269
270 <section><info><title>No <code>vector::at</code>, <code>deque::at</code>, <code>string::at</code></title></info>
271
272
273 <para>
274   One solution is to add an autoconf-test for this:
275 </para>
276
277 <programlisting>
278 AC_MSG_CHECKING(for container::at)
279 AC_TRY_COMPILE(
280 [
281 #include &lt;vector&gt;
282 #include &lt;deque&gt;
283 #include &lt;string&gt;
284
285 using namespace std;
286 ],
287 [
288 deque&lt;int&gt; test_deque(3);
289 test_deque.at(2);
290 vector&lt;int&gt; test_vector(2);
291 test_vector.at(1);
292 string test_string(<quote>test_string</quote>);
293 test_string.at(3);
294 ],
295 [AC_MSG_RESULT(yes)
296 AC_DEFINE(HAVE_CONTAINER_AT)],
297 [AC_MSG_RESULT(no)])
298 </programlisting>
299
300 <para>
301   If you are using other (non-GNU) compilers it might be a good idea
302   to check for <code>string::at</code> separately.
303 </para>
304
305 </section>
306
307 <section><info><title>No <code>std::char_traits&lt;char&gt;::eof</code></title></info>
308
309
310 <para>
311   Use some kind of autoconf test, plus this:
312 </para>
313
314 <programlisting>
315 #ifdef HAVE_CHAR_TRAITS
316 #define CPP_EOF std::char_traits&lt;char&gt;::eof()
317 #else
318 #define CPP_EOF EOF
319 #endif
320 </programlisting>
321
322 </section>
323
324 <section><info><title>No <code>string::clear</code></title></info>
325
326
327 <para>
328   There are two functions for deleting the contents of a string:
329   <code>clear</code> and <code>erase</code> (the latter returns the
330   string).
331 </para>
332
333 <programlisting>
334 void
335 clear() { _M_mutate(0, this-&gt;size(), 0); }
336 </programlisting>
337
338 <programlisting>
339 basic_string&amp;
340 erase(size_type __pos = 0, size_type __n = npos)
341 {
342   return this-&gt;replace(_M_check(__pos), _M_fold(__pos, __n),
343                           _M_data(), _M_data());
344 }
345 </programlisting>
346
347 <para>
348   Unfortunately, <code>clear</code> is not implemented in this
349   version, so you should use <code>erase</code> (which is probably
350   faster than <code>operator=(charT*)</code>).
351 </para>
352 </section>
353
354 <section><info><title>
355   Removal of <code>ostream::form</code> and <code>istream::scan</code>
356   extensions
357 </title></info>
358
359
360 <para>
361   These are no longer supported. Please use stringstreams instead.
362 </para>
363 </section>
364
365 <section><info><title>No <code>basic_stringbuf</code>, <code>basic_stringstream</code></title></info>
366
367
368 <para>
369   Although the ISO standard <code>i/ostringstream</code>-classes are
370   provided, (<filename class="headerfile">sstream</filename>), for
371   compatibility with older implementations the pre-ISO
372   <code>i/ostrstream</code> (<filename class="headerfile">strstream</filename>) interface is also provided,
373   with these caveats:
374 </para>
375
376 <itemizedlist>
377   <listitem>
378     <para>
379       <code>strstream</code> is considered to be deprecated
380     </para>
381   </listitem>
382   <listitem>
383     <para>
384       <code>strstream</code> is limited to <code>char</code>
385     </para>
386   </listitem>
387   <listitem>
388     <para>
389       with <code>ostringstream</code> you don't have to take care of
390       terminating the string or freeing its memory
391     </para>
392   </listitem>
393   <listitem>
394     <para>
395       <code>istringstream</code> can be re-filled (clear();
396       str(input);)
397     </para>
398   </listitem>
399 </itemizedlist>
400
401 <para>
402   You can then use output-stringstreams like this:
403 </para>
404
405 <programlisting>
406 #ifdef HAVE_SSTREAM
407 # include &lt;sstream&gt;
408 #else
409 # include &lt;strstream&gt;
410 #endif
411
412 #ifdef HAVE_SSTREAM
413   std::ostringstream oss;
414 #else
415   std::ostrstream oss;
416 #endif
417
418 oss &lt;&lt; <quote>Name=</quote> &lt;&lt; m_name &lt;&lt; <quote>, number=</quote> &lt;&lt; m_number &lt;&lt; std::endl;
419 ...
420 #ifndef HAVE_SSTREAM
421   oss &lt;&lt; std::ends; // terminate the char*-string
422 #endif
423
424 // str() returns char* for ostrstream and a string for ostringstream
425 // this also causes ostrstream to think that the buffer's memory
426 // is yours
427 m_label.set_text(oss.str());
428 #ifndef HAVE_SSTREAM
429   // let the ostrstream take care of freeing the memory
430   oss.freeze(false);
431 #endif
432 </programlisting>
433
434 <para>
435       Input-stringstreams can be used similarly:
436 </para>
437
438 <programlisting>
439 std::string input;
440 ...
441 #ifdef HAVE_SSTREAM
442 std::istringstream iss(input);
443 #else
444 std::istrstream iss(input.c_str());
445 #endif
446
447 int i;
448 iss &gt;&gt; i;
449 </programlisting>
450
451 <para> One (the only?) restriction is that an istrstream cannot be re-filled:
452 </para>
453
454 <programlisting>
455 std::istringstream iss(numerator);
456 iss &gt;&gt; m_num;
457 // this is not possible with istrstream
458 iss.clear();
459 iss.str(denominator);
460 iss &gt;&gt; m_den;
461 </programlisting>
462
463 <para>
464 If you don't care about speed, you can put these conversions in
465       a template-function:
466 </para>
467 <programlisting>
468 template &lt;class X&gt;
469 void fromString(const string&amp; input, X&amp; any)
470 {
471 #ifdef HAVE_SSTREAM
472 std::istringstream iss(input);
473 #else
474 std::istrstream iss(input.c_str());
475 #endif
476 X temp;
477 iss &gt;&gt; temp;
478 if (iss.fail())
479 throw runtime_error(..)
480 any = temp;
481 }
482 </programlisting>
483
484 <para>
485   Another example of using stringstreams is in <link linkend="strings.string.shrink">this howto</link>.
486 </para>
487
488 <para> There is additional information in the libstdc++-v2 info files, in
489 particular <quote>info iostream</quote>.
490 </para>
491 </section>
492
493 <section><info><title>Little or no wide character support</title></info>
494   
495   <para>
496     Classes <classname>wstring</classname> and
497     <classname>char_traits&lt;wchar_t&gt;</classname> are
498     not supported.
499   </para>
500 </section>
501
502 <section><info><title>No templatized iostreams</title></info>
503   
504   <para>
505     Classes <classname>wfilebuf</classname> and
506     <classname>wstringstream</classname> are not supported.
507   </para>
508 </section>
509
510 <section><info><title>Thread safety issues</title></info>
511
512
513   <para>
514     Earlier GCC releases had a somewhat different approach to
515     threading configuration and proper compilation.  Before GCC 3.0,
516     configuration of the threading model was dictated by compiler
517     command-line options and macros (both of which were somewhat
518     thread-implementation and port-specific).  There were no
519     guarantees related to being able to link code compiled with one
520     set of options and macro setting with another set.
521   </para>
522
523   <para>
524     For GCC 3.0, configuration of the threading model used with
525     libraries and user-code is performed when GCC is configured and
526     built using the --enable-threads and --disable-threads options.
527     The ABI is stable for symbol name-mangling and limited functional
528     compatibility exists between code compiled under different
529     threading models.
530   </para>
531
532    <para>
533      The libstdc++ library has been designed so that it can be used in
534      multithreaded applications (with libstdc++-v2 this was only true
535      of the STL parts.)  The first problem is finding a
536      <emphasis>fast</emphasis> method of implementation portable to
537      all platforms.  Due to historical reasons, some of the library is
538      written against per-CPU-architecture spinlocks and other parts
539      against the gthr.h abstraction layer which is provided by gcc.  A
540      minor problem that pops up every so often is different
541      interpretations of what "thread-safe" means for a
542      library (not a general program).  We currently use the <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.sgi.com/tech/stl/thread_safety.html">same
543      definition that SGI</link> uses for their STL subset.  However,
544      the exception for read-only containers only applies to the STL
545      components. This definition is widely-used and something similar
546      will be used in the next version of the C++ standard library.
547    </para>
548
549    <para>
550      Here is a small link farm to threads (no pun) in the mail
551      archives that discuss the threading problem.  Each link is to the
552      first relevant message in the thread; from there you can use
553      "Thread Next" to move down the thread.  This farm is in
554      latest-to-oldest order.
555    </para>
556
557       <itemizedlist>
558         <listitem>
559           <para>
560             Our threading expert Loren gives a breakdown of <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://gcc.gnu.org/ml/libstdc++/2001-10/msg00024.html">the
561             six situations involving threads</link> for the 3.0
562             release series.
563           </para>
564       </listitem>
565         <listitem>
566           <para>
567             <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://gcc.gnu.org/ml/libstdc++/2001-05/msg00384.html">
568         This message</link> inspired a recent updating of issues with
569         threading and the SGI STL library.  It also contains some
570         example POSIX-multithreaded STL code.
571           </para>
572         </listitem>
573       </itemizedlist>
574
575    <para>
576      (A large selection of links to older messages has been removed;
577      many of the messages from 1999 were lost in a disk crash, and the
578      few people with access to the backup tapes have been too swamped
579      with work to restore them.  Many of the points have been
580      superseded anyhow.)
581    </para>
582 </section>
583
584 </section>
585
586 <section xml:id="backwards.third"><info><title>Third</title></info>
587
588
589 <para> The third generation GNU C++ library is called libstdc++, or
590 libstdc++-v3.
591 </para>
592
593       <para>The subset commonly known as the Standard Template Library
594          (chapters 23 through 25, mostly) is adapted from the final release
595          of the SGI STL (version 3.3), with extensive changes.
596       </para>
597
598       <para>A more formal description of the V3 goals can be found in the
599          official <link linkend="contrib.design_notes">design document</link>.
600       </para>
601
602 <para>Portability notes and known implementation limitations are as follows.</para>
603
604 <section><info><title>Pre-ISO headers moved to backwards or removed</title></info>
605
606
607 <para> The pre-ISO C++ headers
608       (<code>iostream.h</code>, <code>defalloc.h</code> etc.) are
609       available, unlike previous libstdc++ versions, but inclusion
610       generates a warning that you are using deprecated headers.
611 </para>
612
613     <para>This compatibility layer is constructed by including the
614     standard C++ headers, and injecting any items in
615     <code>std::</code> into the global namespace.
616    </para>
617    <para>For those of you new to ISO C++ (welcome, time travelers!), no,
618       that isn't a typo. Yes, the headers really have new names.
619       Marshall Cline's C++ FAQ Lite has a good explanation in <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.4">item
620       [27.4]</link>.
621    </para>
622
623 <para> Some include adjustment may be required. What follows is an
624 autoconf test that defines <code>PRE_STDCXX_HEADERS</code> when they
625 exist.</para>
626
627 <programlisting>
628 # AC_HEADER_PRE_STDCXX
629 AC_DEFUN([AC_HEADER_PRE_STDCXX], [
630   AC_CACHE_CHECK(for pre-ISO C++ include files,
631   ac_cv_cxx_pre_stdcxx,
632   [AC_LANG_SAVE
633   AC_LANG_CPLUSPLUS
634   ac_save_CXXFLAGS="$CXXFLAGS"
635   CXXFLAGS="$CXXFLAGS -Wno-deprecated"
636
637   # Omit defalloc.h, as compilation with newer compilers is problematic.
638   AC_TRY_COMPILE([
639   #include &lt;new.h&gt;
640   #include &lt;iterator.h&gt;
641   #include &lt;alloc.h&gt;
642   #include &lt;set.h&gt;
643   #include &lt;hashtable.h&gt;
644   #include &lt;hash_set.h&gt;
645   #include &lt;fstream.h&gt;
646   #include &lt;tempbuf.h&gt;
647   #include &lt;istream.h&gt;
648   #include &lt;bvector.h&gt;
649   #include &lt;stack.h&gt;
650   #include &lt;rope.h&gt;
651   #include &lt;complex.h&gt;
652   #include &lt;ostream.h&gt;
653   #include &lt;heap.h&gt;
654   #include &lt;iostream.h&gt;
655   #include &lt;function.h&gt;
656   #include &lt;multimap.h&gt;
657   #include &lt;pair.h&gt;
658   #include &lt;stream.h&gt;
659   #include &lt;iomanip.h&gt;
660   #include &lt;slist.h&gt;
661   #include &lt;tree.h&gt;
662   #include &lt;vector.h&gt;
663   #include &lt;deque.h&gt;
664   #include &lt;multiset.h&gt;
665   #include &lt;list.h&gt;
666   #include &lt;map.h&gt;
667   #include &lt;algobase.h&gt;
668   #include &lt;hash_map.h&gt;
669   #include &lt;algo.h&gt;
670   #include &lt;queue.h&gt;
671   #include &lt;streambuf.h&gt;
672   ],,
673   ac_cv_cxx_pre_stdcxx=yes, ac_cv_cxx_pre_stdcxx=no)
674   CXXFLAGS="$ac_save_CXXFLAGS"
675   AC_LANG_RESTORE
676   ])
677   if test "$ac_cv_cxx_pre_stdcxx" = yes; then
678     AC_DEFINE(PRE_STDCXX_HEADERS,,[Define if pre-ISO C++ header files are present. ])
679   fi
680 ])
681 </programlisting>
682
683 <para>Porting between pre-ISO headers and ISO headers is simple: headers
684 like <filename class="headerfile">vector.h</filename> can be replaced with <filename class="headerfile">vector</filename> and a using
685 directive <code>using namespace std;</code> can be put at the global
686 scope. This should be enough to get this code compiling, assuming the
687 other usage is correct.
688 </para>
689 </section>
690
691 <section><info><title>Extension headers hash_map, hash_set moved to ext or backwards</title></info>
692
693
694       <para>At this time most of the features of the SGI STL extension have been
695          replaced by standardized libraries.
696          In particular, the unordered_map and unordered_set containers of TR1
697          are suitable replacement for the non-standard hash_map and hash_set
698          containers in the SGI STL.
699       </para>
700 <para> Header files <filename class="headerfile">hash_map</filename> and <filename class="headerfile">hash_set</filename> moved
701 to <filename class="headerfile">ext/hash_map</filename> and  <filename class="headerfile">ext/hash_set</filename>,
702 respectively. At the same time, all types in these files are enclosed
703 in <code>namespace __gnu_cxx</code>. Later versions move deprecate
704 these files, and suggest using TR1's  <filename class="headerfile">unordered_map</filename>
705 and  <filename class="headerfile">unordered_set</filename> instead.
706 </para>
707
708       <para>The extensions are no longer in the global or <code>std</code>
709          namespaces, instead they are declared in the <code>__gnu_cxx</code>
710          namespace. For maximum portability, consider defining a namespace
711          alias to use to talk about extensions, e.g.:
712       </para>
713       <programlisting>
714       #ifdef __GNUC__
715       #if __GNUC__ &lt; 3
716         #include &lt;hash_map.h&gt;
717         namespace extension { using ::hash_map; }; // inherit globals
718       #else
719         #include &lt;backward/hash_map&gt;
720         #if __GNUC__ == 3 &amp;&amp; __GNUC_MINOR__ == 0
721           namespace extension = std;               // GCC 3.0
722         #else
723           namespace extension = ::__gnu_cxx;       // GCC 3.1 and later
724         #endif
725       #endif
726       #else      // ...  there are other compilers, right?
727         namespace extension = std;
728       #endif
729
730       extension::hash_map&lt;int,int&gt; my_map;
731       </programlisting>
732       <para>This is a bit cleaner than defining typedefs for all the
733          instantiations you might need.
734       </para>
735
736
737 <para>The following autoconf tests check for working HP/SGI hash containers.
738 </para>
739
740 <programlisting>
741 # AC_HEADER_EXT_HASH_MAP
742 AC_DEFUN([AC_HEADER_EXT_HASH_MAP], [
743   AC_CACHE_CHECK(for ext/hash_map,
744   ac_cv_cxx_ext_hash_map,
745   [AC_LANG_SAVE
746   AC_LANG_CPLUSPLUS
747   ac_save_CXXFLAGS="$CXXFLAGS"
748   CXXFLAGS="$CXXFLAGS -Werror"
749   AC_TRY_COMPILE([#include &lt;ext/hash_map&gt;], [using __gnu_cxx::hash_map;],
750   ac_cv_cxx_ext_hash_map=yes, ac_cv_cxx_ext_hash_map=no)
751   CXXFLAGS="$ac_save_CXXFLAGS"
752   AC_LANG_RESTORE
753   ])
754   if test "$ac_cv_cxx_ext_hash_map" = yes; then
755     AC_DEFINE(HAVE_EXT_HASH_MAP,,[Define if ext/hash_map is present. ])
756   fi
757 ])
758 </programlisting>
759
760 <programlisting>
761 # AC_HEADER_EXT_HASH_SET
762 AC_DEFUN([AC_HEADER_EXT_HASH_SET], [
763   AC_CACHE_CHECK(for ext/hash_set,
764   ac_cv_cxx_ext_hash_set,
765   [AC_LANG_SAVE
766   AC_LANG_CPLUSPLUS
767   ac_save_CXXFLAGS="$CXXFLAGS"
768   CXXFLAGS="$CXXFLAGS -Werror"
769   AC_TRY_COMPILE([#include &lt;ext/hash_set&gt;], [using __gnu_cxx::hash_set;],
770   ac_cv_cxx_ext_hash_set=yes, ac_cv_cxx_ext_hash_set=no)
771   CXXFLAGS="$ac_save_CXXFLAGS"
772   AC_LANG_RESTORE
773   ])
774   if test "$ac_cv_cxx_ext_hash_set" = yes; then
775     AC_DEFINE(HAVE_EXT_HASH_SET,,[Define if ext/hash_set is present. ])
776   fi
777 ])
778 </programlisting>
779 </section>
780
781 <section><info><title>No <code>ios::nocreate/ios::noreplace</code>.
782 </title></info>
783
784
785 <para> The existence of <code>ios::nocreate</code> being used for
786 input-streams has been confirmed, most probably because the author
787 thought it would be more correct to specify nocreate explicitly.  So
788 it can be left out for input-streams.
789 </para>
790
791 <para>For output streams, <quote>nocreate</quote> is probably the default,
792 unless you specify <code>std::ios::trunc</code> ? To be safe, you can
793 open the file for reading, check if it has been opened, and then
794 decide whether you want to create/replace or not. To my knowledge,
795 even older implementations support <code>app</code>, <code>ate</code>
796 and <code>trunc</code> (except for <code>app</code> ?).
797 </para>
798 </section>
799
800 <section><info><title>
801 No <code>stream::attach(int fd)</code>
802 </title></info>
803
804
805 <para>
806       Phil Edwards writes: It was considered and rejected for the ISO
807       standard.  Not all environments use file descriptors.  Of those
808       that do, not all of them use integers to represent them.
809     </para>
810
811 <para>
812       For a portable solution (among systems which use
813       file descriptors), you need to implement a subclass of
814       <code>std::streambuf</code> (or
815       <code>std::basic_streambuf&lt;..&gt;</code>) which opens a file
816       given a descriptor, and then pass an instance of this to the
817       stream-constructor.
818     </para>
819
820 <para>
821       An extension is available that implements this.
822       <filename class="headerfile">ext/stdio_filebuf.h</filename> contains a derived class called
823       <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a00074.html"><code>__gnu_cxx::stdio_filebuf</code></link>.
824       This class can be constructed from a C <code>FILE*</code> or a file
825       descriptor, and provides the <code>fd()</code> function.
826     </para>
827
828 <para>
829  For another example of this, refer to
830       <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.josuttis.com/cppcode/fdstream.html">fdstream example</link>
831       by Nicolai Josuttis.
832 </para>
833 </section>
834
835 <section><info><title>
836 Support for C++98 dialect.
837 </title></info>
838
839
840 <para>Check for complete library coverage of the C++1998/2003 standard.
841 </para>
842
843 <programlisting>
844 # AC_HEADER_STDCXX_98
845 AC_DEFUN([AC_HEADER_STDCXX_98], [
846   AC_CACHE_CHECK(for ISO C++ 98 include files,
847   ac_cv_cxx_stdcxx_98,
848   [AC_LANG_SAVE
849   AC_LANG_CPLUSPLUS
850   AC_TRY_COMPILE([
851     #include &lt;cassert&gt;
852     #include &lt;cctype&gt;
853     #include &lt;cerrno&gt;
854     #include &lt;cfloat&gt;
855     #include &lt;ciso646&gt;
856     #include &lt;climits&gt;
857     #include &lt;clocale&gt;
858     #include &lt;cmath&gt;
859     #include &lt;csetjmp&gt;
860     #include &lt;csignal&gt;
861     #include &lt;cstdarg&gt;
862     #include &lt;cstddef&gt;
863     #include &lt;cstdio&gt;
864     #include &lt;cstdlib&gt;
865     #include &lt;cstring&gt;
866     #include &lt;ctime&gt;
867
868     #include &lt;algorithm&gt;
869     #include &lt;bitset&gt;
870     #include &lt;complex&gt;
871     #include &lt;deque&gt;
872     #include &lt;exception&gt;
873     #include &lt;fstream&gt;
874     #include &lt;functional&gt;
875     #include &lt;iomanip&gt;
876     #include &lt;ios&gt;
877     #include &lt;iosfwd&gt;
878     #include &lt;iostream&gt;
879     #include &lt;istream&gt;
880     #include &lt;iterator&gt;
881     #include &lt;limits&gt;
882     #include &lt;list&gt;
883     #include &lt;locale&gt;
884     #include &lt;map&gt;
885     #include &lt;memory&gt;
886     #include &lt;new&gt;
887     #include &lt;numeric&gt;
888     #include &lt;ostream&gt;
889     #include &lt;queue&gt;
890     #include &lt;set&gt;
891     #include &lt;sstream&gt;
892     #include &lt;stack&gt;
893     #include &lt;stdexcept&gt;
894     #include &lt;streambuf&gt;
895     #include &lt;string&gt;
896     #include &lt;typeinfo&gt;
897     #include &lt;utility&gt;
898     #include &lt;valarray&gt;
899     #include &lt;vector&gt;
900   ],,
901   ac_cv_cxx_stdcxx_98=yes, ac_cv_cxx_stdcxx_98=no)
902   AC_LANG_RESTORE
903   ])
904   if test "$ac_cv_cxx_stdcxx_98" = yes; then
905     AC_DEFINE(STDCXX_98_HEADERS,,[Define if ISO C++ 1998 header files are present. ])
906   fi
907 ])
908 </programlisting>
909 </section>
910
911 <section><info><title>
912 Support for C++TR1 dialect.
913 </title></info>
914
915
916 <para>Check for library coverage of the TR1 standard.
917 </para>
918
919 <programlisting>
920 # AC_HEADER_STDCXX_TR1
921 AC_DEFUN([AC_HEADER_STDCXX_TR1], [
922   AC_CACHE_CHECK(for ISO C++ TR1 include files,
923   ac_cv_cxx_stdcxx_tr1,
924   [AC_LANG_SAVE
925   AC_LANG_CPLUSPLUS
926   AC_TRY_COMPILE([
927   #include &lt;tr1/array&gt;
928   #include &lt;tr1/ccomplex&gt;
929   #include &lt;tr1/cctype&gt;
930   #include &lt;tr1/cfenv&gt;
931   #include &lt;tr1/cfloat&gt;
932   #include &lt;tr1/cinttypes&gt;
933   #include &lt;tr1/climits&gt;
934   #include &lt;tr1/cmath&gt;
935   #include &lt;tr1/complex&gt;
936   #include &lt;tr1/cstdarg&gt;
937   #include &lt;tr1/cstdbool&gt;
938   #include &lt;tr1/cstdint&gt;
939   #include &lt;tr1/cstdio&gt;
940   #include &lt;tr1/cstdlib&gt;
941   #include &lt;tr1/ctgmath&gt;
942   #include &lt;tr1/ctime&gt;
943   #include &lt;tr1/cwchar&gt;
944   #include &lt;tr1/cwctype&gt;
945   #include &lt;tr1/functional&gt;
946   #include &lt;tr1/memory&gt;
947   #include &lt;tr1/random&gt;
948   #include &lt;tr1/regex&gt;
949   #include &lt;tr1/tuple&gt;
950   #include &lt;tr1/type_traits&gt;
951   #include &lt;tr1/unordered_set&gt;
952   #include &lt;tr1/unordered_map&gt;
953   #include &lt;tr1/utility&gt;
954   ],,
955   ac_cv_cxx_stdcxx_tr1=yes, ac_cv_cxx_stdcxx_tr1=no)
956   AC_LANG_RESTORE
957   ])
958   if test "$ac_cv_cxx_stdcxx_tr1" = yes; then
959     AC_DEFINE(STDCXX_TR1_HEADERS,,[Define if ISO C++ TR1 header files are present. ])
960   fi
961 ])
962 </programlisting>
963
964 <para>An alternative is to check just for specific TR1 includes, such as &lt;unordered_map&gt; and &lt;unordered_set&gt;.
965 </para>
966
967 <programlisting>
968 # AC_HEADER_TR1_UNORDERED_MAP
969 AC_DEFUN([AC_HEADER_TR1_UNORDERED_MAP], [
970   AC_CACHE_CHECK(for tr1/unordered_map,
971   ac_cv_cxx_tr1_unordered_map,
972   [AC_LANG_SAVE
973   AC_LANG_CPLUSPLUS
974   AC_TRY_COMPILE([#include &lt;tr1/unordered_map&gt;], [using std::tr1::unordered_map;],
975   ac_cv_cxx_tr1_unordered_map=yes, ac_cv_cxx_tr1_unordered_map=no)
976   AC_LANG_RESTORE
977   ])
978   if test "$ac_cv_cxx_tr1_unordered_map" = yes; then
979     AC_DEFINE(HAVE_TR1_UNORDERED_MAP,,[Define if tr1/unordered_map is present. ])
980   fi
981 ])
982 </programlisting>
983
984 <programlisting>
985 # AC_HEADER_TR1_UNORDERED_SET
986 AC_DEFUN([AC_HEADER_TR1_UNORDERED_SET], [
987   AC_CACHE_CHECK(for tr1/unordered_set,
988   ac_cv_cxx_tr1_unordered_set,
989   [AC_LANG_SAVE
990   AC_LANG_CPLUSPLUS
991   AC_TRY_COMPILE([#include &lt;tr1/unordered_set&gt;], [using std::tr1::unordered_set;],
992   ac_cv_cxx_tr1_unordered_set=yes, ac_cv_cxx_tr1_unordered_set=no)
993   AC_LANG_RESTORE
994   ])
995   if test "$ac_cv_cxx_tr1_unordered_set" = yes; then
996     AC_DEFINE(HAVE_TR1_UNORDERED_SET,,[Define if tr1/unordered_set is present. ])
997   fi
998 ])
999 </programlisting>
1000 </section>
1001
1002
1003 <section><info><title>
1004 Support for C++0x dialect.
1005 </title></info>
1006
1007
1008 <para>Check for baseline language coverage in the compiler for the C++0xstandard.
1009 </para>
1010
1011 <programlisting>
1012 # AC_COMPILE_STDCXX_OX
1013 AC_DEFUN([AC_COMPILE_STDCXX_0X], [
1014   AC_CACHE_CHECK(if g++ supports C++0x features without additional flags,
1015   ac_cv_cxx_compile_cxx0x_native,
1016   [AC_LANG_SAVE
1017   AC_LANG_CPLUSPLUS
1018   AC_TRY_COMPILE([
1019   template &lt;typename T&gt;
1020     struct check
1021     {
1022       static_assert(sizeof(int) &lt;= sizeof(T), "not big enough");
1023     };
1024
1025     typedef check&lt;check&lt;bool&gt;&gt; right_angle_brackets;
1026
1027     int a;
1028     decltype(a) b;
1029
1030     typedef check&lt;int&gt; check_type;
1031     check_type c;
1032     check_type&amp;&amp; cr = c;],,
1033   ac_cv_cxx_compile_cxx0x_native=yes, ac_cv_cxx_compile_cxx0x_native=no)
1034   AC_LANG_RESTORE
1035   ])
1036
1037   AC_CACHE_CHECK(if g++ supports C++0x features with -std=c++0x,
1038   ac_cv_cxx_compile_cxx0x_cxx,
1039   [AC_LANG_SAVE
1040   AC_LANG_CPLUSPLUS
1041   ac_save_CXXFLAGS="$CXXFLAGS"
1042   CXXFLAGS="$CXXFLAGS -std=c++0x"
1043   AC_TRY_COMPILE([
1044   template &lt;typename T&gt;
1045     struct check
1046     {
1047       static_assert(sizeof(int) &lt;= sizeof(T), "not big enough");
1048     };
1049
1050     typedef check&lt;check&lt;bool&gt;&gt; right_angle_brackets;
1051
1052     int a;
1053     decltype(a) b;
1054
1055     typedef check&lt;int&gt; check_type;
1056     check_type c;
1057     check_type&amp;&amp; cr = c;],,
1058   ac_cv_cxx_compile_cxx0x_cxx=yes, ac_cv_cxx_compile_cxx0x_cxx=no)
1059   CXXFLAGS="$ac_save_CXXFLAGS"
1060   AC_LANG_RESTORE
1061   ])
1062
1063   AC_CACHE_CHECK(if g++ supports C++0x features with -std=gnu++0x,
1064   ac_cv_cxx_compile_cxx0x_gxx,
1065   [AC_LANG_SAVE
1066   AC_LANG_CPLUSPLUS
1067   ac_save_CXXFLAGS="$CXXFLAGS"
1068   CXXFLAGS="$CXXFLAGS -std=gnu++0x"
1069   AC_TRY_COMPILE([
1070   template &lt;typename T&gt;
1071     struct check
1072     {
1073       static_assert(sizeof(int) &lt;= sizeof(T), "not big enough");
1074     };
1075
1076     typedef check&lt;check&lt;bool&gt;&gt; right_angle_brackets;
1077
1078     int a;
1079     decltype(a) b;
1080
1081     typedef check&lt;int&gt; check_type;
1082     check_type c;
1083     check_type&amp;&amp; cr = c;],,
1084   ac_cv_cxx_compile_cxx0x_gxx=yes, ac_cv_cxx_compile_cxx0x_gxx=no)
1085   CXXFLAGS="$ac_save_CXXFLAGS"
1086   AC_LANG_RESTORE
1087   ])
1088
1089   if test "$ac_cv_cxx_compile_cxx0x_native" = yes ||
1090      test "$ac_cv_cxx_compile_cxx0x_cxx" = yes ||
1091      test "$ac_cv_cxx_compile_cxx0x_gxx" = yes; then
1092     AC_DEFINE(HAVE_STDCXX_0X,,[Define if g++ supports C++0x features. ])
1093   fi
1094 ])
1095 </programlisting>
1096
1097
1098 <para>Check for library coverage of the C++0xstandard.
1099 </para>
1100
1101 <programlisting>
1102 # AC_HEADER_STDCXX_0X
1103 AC_DEFUN([AC_HEADER_STDCXX_0X], [
1104   AC_CACHE_CHECK(for ISO C++ 0x include files,
1105   ac_cv_cxx_stdcxx_0x,
1106   [AC_REQUIRE([AC_COMPILE_STDCXX_0X])
1107   AC_LANG_SAVE
1108   AC_LANG_CPLUSPLUS
1109   ac_save_CXXFLAGS="$CXXFLAGS"
1110   CXXFLAGS="$CXXFLAGS -std=gnu++0x"
1111
1112   AC_TRY_COMPILE([
1113     #include &lt;cassert&gt;
1114     #include &lt;ccomplex&gt;
1115     #include &lt;cctype&gt;
1116     #include &lt;cerrno&gt;
1117     #include &lt;cfenv&gt;
1118     #include &lt;cfloat&gt;
1119     #include &lt;cinttypes&gt;
1120     #include &lt;ciso646&gt;
1121     #include &lt;climits&gt;
1122     #include &lt;clocale&gt;
1123     #include &lt;cmath&gt;
1124     #include &lt;csetjmp&gt;
1125     #include &lt;csignal&gt;
1126     #include &lt;cstdarg&gt;
1127     #include &lt;cstdbool&gt;
1128     #include &lt;cstddef&gt;
1129     #include &lt;cstdint&gt;
1130     #include &lt;cstdio&gt;
1131     #include &lt;cstdlib&gt;
1132     #include &lt;cstring&gt;
1133     #include &lt;ctgmath&gt;
1134     #include &lt;ctime&gt;
1135     #include &lt;cwchar&gt;
1136     #include &lt;cwctype&gt;
1137
1138     #include &lt;algorithm&gt;
1139     #include &lt;array&gt;
1140     #include &lt;bitset&gt;
1141     #include &lt;complex&gt;
1142     #include &lt;deque&gt;
1143     #include &lt;exception&gt;
1144     #include &lt;fstream&gt;
1145     #include &lt;functional&gt;
1146     #include &lt;iomanip&gt;
1147     #include &lt;ios&gt;
1148     #include &lt;iosfwd&gt;
1149     #include &lt;iostream&gt;
1150     #include &lt;istream&gt;
1151     #include &lt;iterator&gt;
1152     #include &lt;limits&gt;
1153     #include &lt;list&gt;
1154     #include &lt;locale&gt;
1155     #include &lt;map&gt;
1156     #include &lt;memory&gt;
1157     #include &lt;new&gt;
1158     #include &lt;numeric&gt;
1159     #include &lt;ostream&gt;
1160     #include &lt;queue&gt;
1161     #include &lt;random&gt;
1162     #include &lt;regex&gt;
1163     #include &lt;set&gt;
1164     #include &lt;sstream&gt;
1165     #include &lt;stack&gt;
1166     #include &lt;stdexcept&gt;
1167     #include &lt;streambuf&gt;
1168     #include &lt;string&gt;
1169     #include &lt;tuple&gt;
1170     #include &lt;typeinfo&gt;
1171     #include &lt;type_traits&gt;
1172     #include &lt;unordered_map&gt;
1173     #include &lt;unordered_set&gt;
1174     #include &lt;utility&gt;
1175     #include &lt;valarray&gt;
1176     #include &lt;vector&gt;
1177   ],,
1178   ac_cv_cxx_stdcxx_0x=yes, ac_cv_cxx_stdcxx_0x=no)
1179   AC_LANG_RESTORE
1180   CXXFLAGS="$ac_save_CXXFLAGS"
1181   ])
1182   if test "$ac_cv_cxx_stdcxx_0x" = yes; then
1183     AC_DEFINE(STDCXX_0X_HEADERS,,[Define if ISO C++ 0x header files are present. ])
1184   fi
1185 ])
1186 </programlisting>
1187
1188 <para>As is the case for TR1 support, these autoconf macros can be made for a finer-grained, per-header-file check. For &lt;unordered_map&gt;
1189 </para>
1190
1191 <programlisting>
1192 # AC_HEADER_UNORDERED_MAP
1193 AC_DEFUN([AC_HEADER_UNORDERED_MAP], [
1194   AC_CACHE_CHECK(for unordered_map,
1195   ac_cv_cxx_unordered_map,
1196   [AC_REQUIRE([AC_COMPILE_STDCXX_0X])
1197   AC_LANG_SAVE
1198   AC_LANG_CPLUSPLUS
1199   ac_save_CXXFLAGS="$CXXFLAGS"
1200   CXXFLAGS="$CXXFLAGS -std=gnu++0x"
1201   AC_TRY_COMPILE([#include &lt;unordered_map&gt;], [using std::unordered_map;],
1202   ac_cv_cxx_unordered_map=yes, ac_cv_cxx_unordered_map=no)
1203   CXXFLAGS="$ac_save_CXXFLAGS"
1204   AC_LANG_RESTORE
1205   ])
1206   if test "$ac_cv_cxx_unordered_map" = yes; then
1207     AC_DEFINE(HAVE_UNORDERED_MAP,,[Define if unordered_map is present. ])
1208   fi
1209 ])
1210 </programlisting>
1211
1212 <programlisting>
1213 # AC_HEADER_UNORDERED_SET
1214 AC_DEFUN([AC_HEADER_UNORDERED_SET], [
1215   AC_CACHE_CHECK(for unordered_set,
1216   ac_cv_cxx_unordered_set,
1217   [AC_REQUIRE([AC_COMPILE_STDCXX_0X])
1218   AC_LANG_SAVE
1219   AC_LANG_CPLUSPLUS
1220   ac_save_CXXFLAGS="$CXXFLAGS"
1221   CXXFLAGS="$CXXFLAGS -std=gnu++0x"
1222   AC_TRY_COMPILE([#include &lt;unordered_set&gt;], [using std::unordered_set;],
1223   ac_cv_cxx_unordered_set=yes, ac_cv_cxx_unordered_set=no)
1224   CXXFLAGS="$ac_save_CXXFLAGS"
1225   AC_LANG_RESTORE
1226   ])
1227   if test "$ac_cv_cxx_unordered_set" = yes; then
1228     AC_DEFINE(HAVE_UNORDERED_SET,,[Define if unordered_set is present. ])
1229   fi
1230 ])
1231 </programlisting>
1232 </section>
1233
1234 <section><info><title>
1235   Container::iterator_type is not necessarily Container::value_type*
1236 </title></info>
1237
1238
1239 <para>
1240   This is a change in behavior from the previous version. Now, most
1241   <type>iterator_type</type> typedefs in container classes are POD
1242   objects, not <type>value_type</type> pointers.
1243 </para>
1244 </section>
1245
1246 </section>
1247
1248 <bibliography xml:id="backwards.biblio"><info><title>Bibliography</title></info>
1249
1250
1251   <biblioentry>
1252     <biblioid xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.kegel.com/gcc/gcc4.html" class="uri">
1253     </biblioid>
1254     <citetitle>
1255       Migrating to GCC 4.1
1256     </citetitle>
1257
1258     <author><personname><firstname>Dan</firstname><surname>Kegel</surname></personname></author>
1259   </biblioentry>
1260
1261   <biblioentry>
1262     <biblioid xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://lists.debian.org/debian-gcc/2006/03/msg00405.html" class="uri">
1263     </biblioid>
1264     <citetitle>
1265       Building the Whole Debian Archive with GCC 4.1: A Summary
1266     </citetitle>
1267
1268     <author><personname><firstname>Martin</firstname><surname>Michlmayr</surname></personname></author>
1269   </biblioentry>
1270
1271   <biblioentry>
1272     <biblioid xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://annwm.lbl.gov/~leggett/Atlas/gcc-3.2.html" class="uri">
1273     </biblioid>
1274     <citetitle>
1275       Migration guide for GCC-3.2
1276     </citetitle>
1277
1278   </biblioentry>
1279
1280 </bibliography>
1281
1282 </section>