1 <html><head><meta http-equiv=
"Content-Type" content=
"text/html; charset=UTF-8"><title>Backwards Compatibility
</title><meta name=
"generator" content=
"DocBook XSL-NS Stylesheets V1.76.1"><meta name=
"keywords" content=
"
5 "><meta name=
"keywords" content=
"
9 "><meta name=
"keywords" content=
"
15 "><link rel=
"home" href=
"../index.html" title=
"The GNU C++ Library"><link rel=
"up" href=
"appendix_porting.html" title=
"Appendix B. Porting and Maintenance"><link rel=
"prev" href=
"api.html" title=
"API Evolution and Deprecation History"><link rel=
"next" href=
"appendix_free.html" title=
"Appendix C. Free Software Needs Free Documentation"></head><body bgcolor=
"white" text=
"black" link=
"#0000FF" vlink=
"#840084" alink=
"#0000FF"><div class=
"navheader"><table width=
"100%" summary=
"Navigation header"><tr><th colspan=
"3" align=
"center">Backwards Compatibility
</th></tr><tr><td width=
"20%" align=
"left"><a accesskey=
"p" href=
"api.html">Prev
</a> </td><th width=
"60%" align=
"center">Appendix B.
16 Porting and Maintenance
18 </th><td width=
"20%" align=
"right"> <a accesskey=
"n" href=
"appendix_free.html">Next
</a></td></tr></table><hr></div><div class=
"section" title=
"Backwards Compatibility"><div class=
"titlepage"><div><div><h2 class=
"title" style=
"clear: both"><a name=
"manual.appendix.porting.backwards"></a>Backwards Compatibility
</h2></div></div></div><div class=
"section" title=
"First"><div class=
"titlepage"><div><div><h3 class=
"title"><a name=
"backwards.first"></a>First
</h3></div></div></div><p>The first generation GNU C++ library was called libg++. It was a
19 separate GNU project, although reliably paired with GCC. Rumors imply
20 that it had a working relationship with at least two kinds of
22 </p><p>Some background: libg++ was designed and created when there was no
23 ISO standard to provide guidance. Classes like linked lists are now
24 provided for by
<code class=
"classname">list
<T
></code> and do not need to be
25 created by
<code class=
"function">genclass
</code>. (For that matter, templates exist
26 now and are well-supported, whereas genclass (mostly) predates them.)
27 </p><p>There are other classes in libg++ that are not specified in the
28 ISO Standard (e.g., statistical analysis). While there are a lot of
29 really useful things that are used by a lot of people, the Standards
30 Committee couldn't include everything, and so a lot of those
31 <span class=
"quote">“
<span class=
"quote">obvious
</span>”
</span> classes didn't get included.
32 </p><p>Known Issues include many of the limitations of its immediate ancestor.
</p><p>Portability notes and known implementation limitations are as follows.
</p><div class=
"section" title=
"No ios_base"><div class=
"titlepage"><div><div><h4 class=
"title"><a name=
"backwards.first.ios_base"></a>No
<code class=
"code">ios_base
</code></h4></div></div></div><p> At least some older implementations don't have
<code class=
"code">std::ios_base
</code>, so you should use
<code class=
"code">std::ios::badbit
</code>,
<code class=
"code">std::ios::failbit
</code> and
<code class=
"code">std::ios::eofbit
</code> and
<code class=
"code">std::ios::goodbit
</code>.
33 </p></div><div class=
"section" title=
"No cout in <ostream.h>, no cin in <istream.h>"><div class=
"titlepage"><div><div><h4 class=
"title"><a name=
"backwards.first.cout_cin"></a>No
<code class=
"code">cout
</code> in
<code class=
"filename"><ostream.h
></code>, no
<code class=
"code">cin
</code> in
<code class=
"filename"><istream.h
></code></h4></div></div></div><p>
34 In earlier versions of the standard,
35 <code class=
"filename"><fstream.h
></code>,
36 <code class=
"filename"><ostream.h
></code>
37 and
<code class=
"filename"><istream.h
></code>
39 <code class=
"code">cout
</code>,
<code class=
"code">cin
</code> and so on. ISO C++ specifies that one needs to include
40 <code class=
"filename"><iostream
></code>
41 explicitly to get the required definitions.
42 </p><p> Some include adjustment may be required.
</p><p>This project is no longer maintained or supported, and the sources
43 archived. For the desperate,
44 the
<a class=
"link" href=
"http://gcc.gnu.org/extensions.html" target=
"_top">GCC extensions
45 page
</a> describes where to find the last libg++ source. The code is
46 considered replaced and rewritten.
47 </p></div></div><div class=
"section" title=
"Second"><div class=
"titlepage"><div><div><h3 class=
"title"><a name=
"backwards.second"></a>Second
</h3></div></div></div><p>
48 The second generation GNU C++ library was called libstdc++, or
49 libstdc++-v2. It spans the time between libg++ and pre-ISO C++
50 standardization and is usually associated with the following GCC
51 releases: egcs
1.x, gcc
2.95, and gcc
2.96.
53 The STL portions of this library are based on SGI/HP STL release
3.11.
55 This project is no longer maintained or supported, and the sources
56 archived. The code is considered replaced and rewritten.
58 Portability notes and known implementation limitations are as follows.
59 </p><div class=
"section" title=
"Namespace std:: not supported"><div class=
"titlepage"><div><div><h4 class=
"title"><a name=
"backwards.second.std"></a>Namespace
<code class=
"code">std::
</code> not supported
</h4></div></div></div><p>
60 Some care is required to support C++ compiler and or library
61 implementation that do not have the standard library in
62 <code class=
"code">namespace std
</code>.
64 The following sections list some possible solutions to support compilers
65 that cannot ignore
<code class=
"code">std::
</code>-qualified names.
67 First, see if the compiler has a flag for this. Namespace
68 back-portability-issues are generally not a problem for g++
69 compilers that do not have libstdc++ in
<code class=
"code">std::
</code>, as the
70 compilers use
<code class=
"option">-fno-honor-std
</code> (ignore
71 <code class=
"code">std::
</code>,
<code class=
"code">:: = std::
</code>) by default. That is,
72 the responsibility for enabling or disabling
<code class=
"code">std::
</code> is
73 on the user; the maintainer does not have to care about it. This
74 probably applies to some other compilers as well.
76 Second, experiment with a variety of pre-processor tricks.
78 By defining
<code class=
"code">std
</code> as a macro, fully-qualified namespace
79 calls become global. Volia.
80 </p><pre class=
"programlisting">
81 #ifdef WICKEDLY_OLD_COMPILER
85 Thanks to Juergen Heinzl who posted this solution on gnu.gcc.help.
87 Another pre-processor based approach is to define a macro
88 <code class=
"code">NAMESPACE_STD
</code>, which is defined to either
89 <span class=
"quote">“
<span class=
"quote"> </span>”
</span> or
<span class=
"quote">“
<span class=
"quote">std
</span>”
</span> based on a compile-type
90 test. On GNU systems, this can be done with autotools by means of
91 an autoconf test (see below) for
<code class=
"code">HAVE_NAMESPACE_STD
</code>,
92 then using that to set a value for the
<code class=
"code">NAMESPACE_STD
</code>
93 macro. At that point, one is able to use
94 <code class=
"code">NAMESPACE_STD::string
</code>, which will evaluate to
95 <code class=
"code">std::string
</code> or
<code class=
"code">::string
</code> (i.e., in the
96 global namespace on systems that do not put
<code class=
"code">string
</code> in
97 <code class=
"code">std::
</code>).
98 </p><pre class=
"programlisting">
99 dnl @synopsis AC_CXX_NAMESPACE_STD
101 dnl If the compiler supports namespace std, define
102 dnl HAVE_NAMESPACE_STD.
105 dnl @author Todd Veldhuizen
106 dnl @author Luc Maisonobe
<luc@spaceroots.org
>
107 dnl @version
2004-
02-
04
108 dnl @license AllPermissive
109 AC_DEFUN([AC_CXX_NAMESPACE_STD], [
110 AC_CACHE_CHECK(if g++ supports namespace std,
111 ac_cv_cxx_have_std_namespace,
114 AC_TRY_COMPILE([#include
<iostream
>
115 std::istream
& is = std::cin;],,
116 ac_cv_cxx_have_std_namespace=yes, ac_cv_cxx_have_std_namespace=no)
119 if test
"$ac_cv_cxx_have_std_namespace" = yes; then
120 AC_DEFINE(HAVE_NAMESPACE_STD,,[Define if g++ supports namespace std. ])
123 </pre></div><div class=
"section" title=
"Illegal iterator usage"><div class=
"titlepage"><div><div><h4 class=
"title"><a name=
"backwards.second.iterators"></a>Illegal iterator usage
</h4></div></div></div><p>
124 The following illustrate implementation-allowed illegal iterator
125 use, and then correct use.
126 </p><div class=
"itemizedlist"><ul class=
"itemizedlist" type=
"disc"><li class=
"listitem"><p>
127 you cannot do
<code class=
"code">ostream::operator
<<(iterator)
</code>
128 to print the address of the iterator =
> use
129 <code class=
"code">operator
<< &*iterator
</code> instead
130 </p></li><li class=
"listitem"><p>
131 you cannot clear an iterator's reference (
<code class=
"code">iterator =
132 0</code>) =
> use
<code class=
"code">iterator = iterator_type();
</code>
133 </p></li><li class=
"listitem"><p>
134 <code class=
"code">if (iterator)
</code> won't work any more =
> use
135 <code class=
"code">if (iterator != iterator_type())
</code>
136 </p></li></ul></div></div><div class=
"section" title=
"isspace from <cctype> is a macro"><div class=
"titlepage"><div><div><h4 class=
"title"><a name=
"backwards.second.isspace"></a><code class=
"code">isspace
</code> from
<code class=
"filename"><cctype
></code> is a macro
137 </h4></div></div></div><p>
138 Glibc
2.0.x and
2.1.x define
<code class=
"filename"><ctype.h
></code> functionality as macros
139 (isspace, isalpha etc.).
141 This implementations of libstdc++, however, keep these functions
142 as macros, and so it is not back-portable to use fully qualified
144 </p><pre class=
"programlisting">
145 #include
<cctype
>
146 int main() { std::isspace('X'); }
148 Results in something like this:
149 </p><pre class=
"programlisting">
150 std:: (__ctype_b[(int) ( ( 'X' ) )]
& (unsigned short int) _ISspace ) ;
152 A solution is to modify a header-file so that the compiler tells
153 <code class=
"filename"><ctype.h
></code> to define functions
155 </p><pre class=
"programlisting">
156 // This keeps isalnum, et al from being propagated as macros.
158 # define __NO_CTYPE
1
161 Then, include
<code class=
"filename"><ctype.h
></code>
163 Another problem arises if you put a
<code class=
"code">using namespace
164 std;
</code> declaration at the top, and include
165 <code class=
"filename"><ctype.h
></code>. This will
166 result in ambiguities between the definitions in the global namespace
167 (
<code class=
"filename"><ctype.h
></code>) and the
168 definitions in namespace
<code class=
"code">std::
</code>
169 (
<code class=
"code"><cctype
></code>).
170 </p></div><div class=
"section" title=
"No vector::at, deque::at, string::at"><div class=
"titlepage"><div><div><h4 class=
"title"><a name=
"backwards.second.at"></a>No
<code class=
"code">vector::at
</code>,
<code class=
"code">deque::at
</code>,
<code class=
"code">string::at
</code></h4></div></div></div><p>
171 One solution is to add an autoconf-test for this:
172 </p><pre class=
"programlisting">
173 AC_MSG_CHECKING(for container::at)
176 #include
<vector
>
177 #include
<deque
>
178 #include
<string
>
183 deque
<int
> test_deque(
3);
185 vector
<int
> test_vector(
2);
187 string test_string(
<span class=
"quote">“
<span class=
"quote">test_string
</span>”
</span>);
191 AC_DEFINE(HAVE_CONTAINER_AT)],
194 If you are using other (non-GNU) compilers it might be a good idea
195 to check for
<code class=
"code">string::at
</code> separately.
196 </p></div><div class=
"section" title=
"No std::char_traits<char>::eof"><div class=
"titlepage"><div><div><h4 class=
"title"><a name=
"backwards.second.eof"></a>No
<code class=
"code">std::char_traits
<char
>::eof
</code></h4></div></div></div><p>
197 Use some kind of autoconf test, plus this:
198 </p><pre class=
"programlisting">
199 #ifdef HAVE_CHAR_TRAITS
200 #define CPP_EOF std::char_traits
<char
>::eof()
204 </pre></div><div class=
"section" title=
"No string::clear"><div class=
"titlepage"><div><div><h4 class=
"title"><a name=
"backwards.second.stringclear"></a>No
<code class=
"code">string::clear
</code></h4></div></div></div><p>
205 There are two functions for deleting the contents of a string:
206 <code class=
"code">clear
</code> and
<code class=
"code">erase
</code> (the latter returns the
208 </p><pre class=
"programlisting">
210 clear() { _M_mutate(
0, this-
>size(),
0); }
211 </pre><pre class=
"programlisting">
213 erase(size_type __pos =
0, size_type __n = npos)
215 return this-
>replace(_M_check(__pos), _M_fold(__pos, __n),
216 _M_data(), _M_data());
219 Unfortunately,
<code class=
"code">clear
</code> is not implemented in this
220 version, so you should use
<code class=
"code">erase
</code> (which is probably
221 faster than
<code class=
"code">operator=(charT*)
</code>).
222 </p></div><div class=
"section" title=
"Removal of ostream::form and istream::scan extensions"><div class=
"titlepage"><div><div><h4 class=
"title"><a name=
"backwards.second.ostreamform_istreamscan"></a>
223 Removal of
<code class=
"code">ostream::form
</code> and
<code class=
"code">istream::scan
</code>
225 </h4></div></div></div><p>
226 These are no longer supported. Please use stringstreams instead.
227 </p></div><div class=
"section" title=
"No basic_stringbuf, basic_stringstream"><div class=
"titlepage"><div><div><h4 class=
"title"><a name=
"backwards.second.stringstreams"></a>No
<code class=
"code">basic_stringbuf
</code>,
<code class=
"code">basic_stringstream
</code></h4></div></div></div><p>
228 Although the ISO standard
<code class=
"code">i/ostringstream
</code>-classes are
229 provided, (
<code class=
"filename"><sstream
></code>), for
230 compatibility with older implementations the pre-ISO
231 <code class=
"code">i/ostrstream
</code> (
<code class=
"filename"><strstream
></code>) interface is also provided,
233 </p><div class=
"itemizedlist"><ul class=
"itemizedlist" type=
"disc"><li class=
"listitem"><p>
234 <code class=
"code">strstream
</code> is considered to be deprecated
235 </p></li><li class=
"listitem"><p>
236 <code class=
"code">strstream
</code> is limited to
<code class=
"code">char
</code>
237 </p></li><li class=
"listitem"><p>
238 with
<code class=
"code">ostringstream
</code> you don't have to take care of
239 terminating the string or freeing its memory
240 </p></li><li class=
"listitem"><p>
241 <code class=
"code">istringstream
</code> can be re-filled (clear();
243 </p></li></ul></div><p>
244 You can then use output-stringstreams like this:
245 </p><pre class=
"programlisting">
247 # include
<sstream
>
249 # include
<strstream
>
253 std::ostringstream oss;
258 oss
<< <span class=
"quote">“
<span class=
"quote">Name=
</span>”
</span> << m_name
<< <span class=
"quote">“
<span class=
"quote">, number=
</span>”
</span> << m_number
<< std::endl;
261 oss
<< std::ends; // terminate the char*-string
264 // str() returns char* for ostrstream and a string for ostringstream
265 // this also causes ostrstream to think that the buffer's memory
267 m_label.set_text(oss.str());
269 // let the ostrstream take care of freeing the memory
273 Input-stringstreams can be used similarly:
274 </p><pre class=
"programlisting">
278 std::istringstream iss(input);
280 std::istrstream iss(input.c_str());
285 </pre><p> One (the only?) restriction is that an istrstream cannot be re-filled:
286 </p><pre class=
"programlisting">
287 std::istringstream iss(numerator);
289 // this is not possible with istrstream
291 iss.str(denominator);
294 If you don't care about speed, you can put these conversions in
296 </p><pre class=
"programlisting">
297 template
<class X
>
298 void fromString(const string
& input, X
& any)
301 std::istringstream iss(input);
303 std::istrstream iss(input.c_str());
308 throw runtime_error(..)
312 Another example of using stringstreams is in
<a class=
"link" href=
"strings.html#strings.string.shrink" title=
"Shrink to Fit">this howto
</a>.
313 </p><p> There is additional information in the libstdc++-v2 info files, in
314 particular
<span class=
"quote">“
<span class=
"quote">info iostream
</span>”
</span>.
315 </p></div><div class=
"section" title=
"Little or no wide character support"><div class=
"titlepage"><div><div><h4 class=
"title"><a name=
"backwards.second.wchar"></a>Little or no wide character support
</h4></div></div></div><p>
316 Classes
<code class=
"classname">wstring
</code> and
317 <code class=
"classname">char_traits
<wchar_t
></code> are
319 </p></div><div class=
"section" title=
"No templatized iostreams"><div class=
"titlepage"><div><div><h4 class=
"title"><a name=
"backwards.second.iostream_templates"></a>No templatized iostreams
</h4></div></div></div><p>
320 Classes
<code class=
"classname">wfilebuf
</code> and
321 <code class=
"classname">wstringstream
</code> are not supported.
322 </p></div><div class=
"section" title=
"Thread safety issues"><div class=
"titlepage"><div><div><h4 class=
"title"><a name=
"backwards.second.thread_safety"></a>Thread safety issues
</h4></div></div></div><p>
323 Earlier GCC releases had a somewhat different approach to
324 threading configuration and proper compilation. Before GCC
3.0,
325 configuration of the threading model was dictated by compiler
326 command-line options and macros (both of which were somewhat
327 thread-implementation and port-specific). There were no
328 guarantees related to being able to link code compiled with one
329 set of options and macro setting with another set.
331 For GCC
3.0, configuration of the threading model used with
332 libraries and user-code is performed when GCC is configured and
333 built using the --enable-threads and --disable-threads options.
334 The ABI is stable for symbol name-mangling and limited functional
335 compatibility exists between code compiled under different
338 The libstdc++ library has been designed so that it can be used in
339 multithreaded applications (with libstdc++-v2 this was only true
340 of the STL parts.) The first problem is finding a
341 <span class=
"emphasis"><em>fast
</em></span> method of implementation portable to
342 all platforms. Due to historical reasons, some of the library is
343 written against per-CPU-architecture spinlocks and other parts
344 against the gthr.h abstraction layer which is provided by gcc. A
345 minor problem that pops up every so often is different
346 interpretations of what
"thread-safe" means for a
347 library (not a general program). We currently use the
<a class=
"link" href=
"http://www.sgi.com/tech/stl/thread_safety.html" target=
"_top">same
348 definition that SGI
</a> uses for their STL subset. However,
349 the exception for read-only containers only applies to the STL
350 components. This definition is widely-used and something similar
351 will be used in the next version of the C++ standard library.
353 Here is a small link farm to threads (no pun) in the mail
354 archives that discuss the threading problem. Each link is to the
355 first relevant message in the thread; from there you can use
356 "Thread Next" to move down the thread. This farm is in
357 latest-to-oldest order.
358 </p><div class=
"itemizedlist"><ul class=
"itemizedlist" type=
"disc"><li class=
"listitem"><p>
359 Our threading expert Loren gives a breakdown of
<a class=
"link" href=
"http://gcc.gnu.org/ml/libstdc++/2001-10/msg00024.html" target=
"_top">the
360 six situations involving threads
</a> for the
3.0
362 </p></li><li class=
"listitem"><p>
363 <a class=
"link" href=
"http://gcc.gnu.org/ml/libstdc++/2001-05/msg00384.html" target=
"_top">
364 This message
</a> inspired a recent updating of issues with
365 threading and the SGI STL library. It also contains some
366 example POSIX-multithreaded STL code.
367 </p></li></ul></div><p>
368 (A large selection of links to older messages has been removed;
369 many of the messages from
1999 were lost in a disk crash, and the
370 few people with access to the backup tapes have been too swamped
371 with work to restore them. Many of the points have been
373 </p></div></div><div class=
"section" title=
"Third"><div class=
"titlepage"><div><div><h3 class=
"title"><a name=
"backwards.third"></a>Third
</h3></div></div></div><p> The third generation GNU C++ library is called libstdc++, or
375 </p><p>The subset commonly known as the Standard Template Library
376 (chapters
23 through
25, mostly) is adapted from the final release
377 of the SGI STL (version
3.3), with extensive changes.
378 </p><p>A more formal description of the V3 goals can be found in the
379 official
<a class=
"link" href=
"source_design_notes.html" title=
"Design Notes">design document
</a>.
380 </p><p>Portability notes and known implementation limitations are as follows.
</p><div class=
"section" title=
"Pre-ISO headers moved to backwards or removed"><div class=
"titlepage"><div><div><h4 class=
"title"><a name=
"backwards.third.headers"></a>Pre-ISO headers moved to backwards or removed
</h4></div></div></div><p> The pre-ISO C++ headers
381 (
<code class=
"filename"><iostream.h
></code>,
382 <code class=
"filename"><defalloc.h
></code> etc.) are
383 available, unlike previous libstdc++ versions, but inclusion
384 generates a warning that you are using deprecated headers.
385 </p><p>This compatibility layer is constructed by including the
386 standard C++ headers, and injecting any items in
387 <code class=
"code">std::
</code> into the global namespace.
388 </p><p>For those of you new to ISO C++ (welcome, time travelers!), no,
389 that isn't a typo. Yes, the headers really have new names.
390 Marshall Cline's C++ FAQ Lite has a good explanation in
<a class=
"link" href=
"http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.4" target=
"_top">item
392 </p><p> Some include adjustment may be required. What follows is an
393 autoconf test that defines
<code class=
"code">PRE_STDCXX_HEADERS
</code> when they
394 exist.
</p><pre class=
"programlisting">
395 # AC_HEADER_PRE_STDCXX
396 AC_DEFUN([AC_HEADER_PRE_STDCXX], [
397 AC_CACHE_CHECK(for pre-ISO C++ include files,
398 ac_cv_cxx_pre_stdcxx,
401 ac_save_CXXFLAGS=
"$CXXFLAGS"
402 CXXFLAGS=
"$CXXFLAGS -Wno-deprecated"
404 # Omit defalloc.h, as compilation with newer compilers is problematic.
406 #include
<new.h
>
407 #include
<iterator.h
>
408 #include
<alloc.h
>
409 #include
<set.h
>
410 #include
<hashtable.h
>
411 #include
<hash_set.h
>
412 #include
<fstream.h
>
413 #include
<tempbuf.h
>
414 #include
<istream.h
>
415 #include
<bvector.h
>
416 #include
<stack.h
>
417 #include
<rope.h
>
418 #include
<complex.h
>
419 #include
<ostream.h
>
420 #include
<heap.h
>
421 #include
<iostream.h
>
422 #include
<function.h
>
423 #include
<multimap.h
>
424 #include
<pair.h
>
425 #include
<stream.h
>
426 #include
<iomanip.h
>
427 #include
<slist.h
>
428 #include
<tree.h
>
429 #include
<vector.h
>
430 #include
<deque.h
>
431 #include
<multiset.h
>
432 #include
<list.h
>
433 #include
<map.h
>
434 #include
<algobase.h
>
435 #include
<hash_map.h
>
436 #include
<algo.h
>
437 #include
<queue.h
>
438 #include
<streambuf.h
>
440 ac_cv_cxx_pre_stdcxx=yes, ac_cv_cxx_pre_stdcxx=no)
441 CXXFLAGS=
"$ac_save_CXXFLAGS"
444 if test
"$ac_cv_cxx_pre_stdcxx" = yes; then
445 AC_DEFINE(PRE_STDCXX_HEADERS,,[Define if pre-ISO C++ header files are present. ])
448 </pre><p>Porting between pre-ISO headers and ISO headers is simple: headers
449 like
<code class=
"filename"><vector.h
></code> can be replaced with
<code class=
"filename"><vector
></code> and a using
450 directive
<code class=
"code">using namespace std;
</code> can be put at the global
451 scope. This should be enough to get this code compiling, assuming the
452 other usage is correct.
453 </p></div><div class=
"section" title=
"Extension headers hash_map, hash_set moved to ext or backwards"><div class=
"titlepage"><div><div><h4 class=
"title"><a name=
"backwards.third.hash"></a>Extension headers hash_map, hash_set moved to ext or backwards
</h4></div></div></div><p>At this time most of the features of the SGI STL extension have been
454 replaced by standardized libraries.
455 In particular, the
<code class=
"classname">unordered_map
</code> and
456 <code class=
"classname">unordered_set
</code> containers of TR1 and C++
2011
457 are suitable replacements for the non-standard
458 <code class=
"classname">hash_map
</code> and
<code class=
"classname">hash_set
</code>
459 containers in the SGI STL.
460 </p><p> Header files
<code class=
"filename"><hash_map
></code> and
<code class=
"filename"><hash_set
></code> moved
461 to
<code class=
"filename"><ext/hash_map
></code> and
<code class=
"filename"><ext/hash_set
></code>,
462 respectively. At the same time, all types in these files are enclosed
463 in
<code class=
"code">namespace __gnu_cxx
</code>. Later versions deprecate
464 these files, and suggest using TR1's
<code class=
"filename"><unordered_map
></code>
465 and
<code class=
"filename"><unordered_set
></code> instead.
466 </p><p>The extensions are no longer in the global or
<code class=
"code">std
</code>
467 namespaces, instead they are declared in the
<code class=
"code">__gnu_cxx
</code>
468 namespace. For maximum portability, consider defining a namespace
469 alias to use to talk about extensions, e.g.:
470 </p><pre class=
"programlisting">
473 #include
<hash_map.h
>
474 namespace extension { using ::hash_map; }; // inherit globals
476 #include
<backward/hash_map
>
477 #if __GNUC__ ==
3 && __GNUC_MINOR__ ==
0
478 namespace extension = std; // GCC
3.0
480 namespace extension = ::__gnu_cxx; // GCC
3.1 and later
483 #else // ... there are other compilers, right?
484 namespace extension = std;
487 extension::hash_map
<int,int
> my_map;
488 </pre><p>This is a bit cleaner than defining typedefs for all the
489 instantiations you might need.
490 </p><p>The following autoconf tests check for working HP/SGI hash containers.
491 </p><pre class=
"programlisting">
492 # AC_HEADER_EXT_HASH_MAP
493 AC_DEFUN([AC_HEADER_EXT_HASH_MAP], [
494 AC_CACHE_CHECK(for ext/hash_map,
495 ac_cv_cxx_ext_hash_map,
498 ac_save_CXXFLAGS=
"$CXXFLAGS"
499 CXXFLAGS=
"$CXXFLAGS -Werror"
500 AC_TRY_COMPILE([#include
<ext/hash_map
>], [using __gnu_cxx::hash_map;],
501 ac_cv_cxx_ext_hash_map=yes, ac_cv_cxx_ext_hash_map=no)
502 CXXFLAGS=
"$ac_save_CXXFLAGS"
505 if test
"$ac_cv_cxx_ext_hash_map" = yes; then
506 AC_DEFINE(HAVE_EXT_HASH_MAP,,[Define if ext/hash_map is present. ])
509 </pre><pre class=
"programlisting">
510 # AC_HEADER_EXT_HASH_SET
511 AC_DEFUN([AC_HEADER_EXT_HASH_SET], [
512 AC_CACHE_CHECK(for ext/hash_set,
513 ac_cv_cxx_ext_hash_set,
516 ac_save_CXXFLAGS=
"$CXXFLAGS"
517 CXXFLAGS=
"$CXXFLAGS -Werror"
518 AC_TRY_COMPILE([#include
<ext/hash_set
>], [using __gnu_cxx::hash_set;],
519 ac_cv_cxx_ext_hash_set=yes, ac_cv_cxx_ext_hash_set=no)
520 CXXFLAGS=
"$ac_save_CXXFLAGS"
523 if test
"$ac_cv_cxx_ext_hash_set" = yes; then
524 AC_DEFINE(HAVE_EXT_HASH_SET,,[Define if ext/hash_set is present. ])
527 </pre></div><div class=
"section" title=
"No ios::nocreate/ios::noreplace."><div class=
"titlepage"><div><div><h4 class=
"title"><a name=
"backwards.third.nocreate_noreplace"></a>No
<code class=
"code">ios::nocreate/ios::noreplace
</code>.
528 </h4></div></div></div><p> The existence of
<code class=
"code">ios::nocreate
</code> being used for
529 input-streams has been confirmed, most probably because the author
530 thought it would be more correct to specify nocreate explicitly. So
531 it can be left out for input-streams.
532 </p><p>For output streams,
<span class=
"quote">“
<span class=
"quote">nocreate
</span>”
</span> is probably the default,
533 unless you specify
<code class=
"code">std::ios::trunc
</code> ? To be safe, you can
534 open the file for reading, check if it has been opened, and then
535 decide whether you want to create/replace or not. To my knowledge,
536 even older implementations support
<code class=
"code">app
</code>,
<code class=
"code">ate
</code>
537 and
<code class=
"code">trunc
</code> (except for
<code class=
"code">app
</code> ?).
538 </p></div><div class=
"section" title=
"No stream::attach(int fd)"><div class=
"titlepage"><div><div><h4 class=
"title"><a name=
"backwards.third.streamattach"></a>
539 No
<code class=
"code">stream::attach(int fd)
</code>
540 </h4></div></div></div><p>
541 Phil Edwards writes: It was considered and rejected for the ISO
542 standard. Not all environments use file descriptors. Of those
543 that do, not all of them use integers to represent them.
545 For a portable solution (among systems which use
546 file descriptors), you need to implement a subclass of
547 <code class=
"code">std::streambuf
</code> (or
548 <code class=
"code">std::basic_streambuf
<..
></code>) which opens a file
549 given a descriptor, and then pass an instance of this to the
552 An extension is available that implements this.
553 <code class=
"filename"><ext/stdio_filebuf.h
></code> contains a derived class called
554 <a class=
"link" href=
"http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a00074.html" target=
"_top"><code class=
"code">__gnu_cxx::stdio_filebuf
</code></a>.
555 This class can be constructed from a C
<code class=
"code">FILE*
</code> or a file
556 descriptor, and provides the
<code class=
"code">fd()
</code> function.
558 For another example of this, refer to
559 <a class=
"link" href=
"http://www.josuttis.com/cppcode/fdstream.html" target=
"_top">fdstream example
</a>
561 </p></div><div class=
"section" title=
"Support for C++98 dialect."><div class=
"titlepage"><div><div><h4 class=
"title"><a name=
"backwards.third.support_cxx98"></a>
562 Support for C++
98 dialect.
563 </h4></div></div></div><p>Check for complete library coverage of the C++
1998/
2003 standard.
564 </p><pre class=
"programlisting">
565 # AC_HEADER_STDCXX_98
566 AC_DEFUN([AC_HEADER_STDCXX_98], [
567 AC_CACHE_CHECK(for ISO C++
98 include files,
572 #include
<cassert
>
573 #include
<cctype
>
574 #include
<cerrno
>
575 #include
<cfloat
>
576 #include
<ciso646
>
577 #include
<climits
>
578 #include
<clocale
>
579 #include
<cmath
>
580 #include
<csetjmp
>
581 #include
<csignal
>
582 #include
<cstdarg
>
583 #include
<cstddef
>
584 #include
<cstdio
>
585 #include
<cstdlib
>
586 #include
<cstring
>
587 #include
<ctime
>
589 #include
<algorithm
>
590 #include
<bitset
>
591 #include
<complex
>
592 #include
<deque
>
593 #include
<exception
>
594 #include
<fstream
>
595 #include
<functional
>
596 #include
<iomanip
>
598 #include
<iosfwd
>
599 #include
<iostream
>
600 #include
<istream
>
601 #include
<iterator
>
602 #include
<limits
>
603 #include
<list
>
604 #include
<locale
>
606 #include
<memory
>
608 #include
<numeric
>
609 #include
<ostream
>
610 #include
<queue
>
612 #include
<sstream
>
613 #include
<stack
>
614 #include
<stdexcept
>
615 #include
<streambuf
>
616 #include
<string
>
617 #include
<typeinfo
>
618 #include
<utility
>
619 #include
<valarray
>
620 #include
<vector
>
622 ac_cv_cxx_stdcxx_98=yes, ac_cv_cxx_stdcxx_98=no)
625 if test
"$ac_cv_cxx_stdcxx_98" = yes; then
626 AC_DEFINE(STDCXX_98_HEADERS,,[Define if ISO C++
1998 header files are present. ])
629 </pre></div><div class=
"section" title=
"Support for C++TR1 dialect."><div class=
"titlepage"><div><div><h4 class=
"title"><a name=
"backwards.third.support_tr1"></a>
630 Support for C++TR1 dialect.
631 </h4></div></div></div><p>Check for library coverage of the TR1 standard.
632 </p><pre class=
"programlisting">
633 # AC_HEADER_STDCXX_TR1
634 AC_DEFUN([AC_HEADER_STDCXX_TR1], [
635 AC_CACHE_CHECK(for ISO C++ TR1 include files,
636 ac_cv_cxx_stdcxx_tr1,
640 #include
<tr1/array
>
641 #include
<tr1/ccomplex
>
642 #include
<tr1/cctype
>
643 #include
<tr1/cfenv
>
644 #include
<tr1/cfloat
>
645 #include
<tr1/cinttypes
>
646 #include
<tr1/climits
>
647 #include
<tr1/cmath
>
648 #include
<tr1/complex
>
649 #include
<tr1/cstdarg
>
650 #include
<tr1/cstdbool
>
651 #include
<tr1/cstdint
>
652 #include
<tr1/cstdio
>
653 #include
<tr1/cstdlib
>
654 #include
<tr1/ctgmath
>
655 #include
<tr1/ctime
>
656 #include
<tr1/cwchar
>
657 #include
<tr1/cwctype
>
658 #include
<tr1/functional
>
659 #include
<tr1/memory
>
660 #include
<tr1/random
>
661 #include
<tr1/regex
>
662 #include
<tr1/tuple
>
663 #include
<tr1/type_traits
>
664 #include
<tr1/unordered_set
>
665 #include
<tr1/unordered_map
>
666 #include
<tr1/utility
>
668 ac_cv_cxx_stdcxx_tr1=yes, ac_cv_cxx_stdcxx_tr1=no)
671 if test
"$ac_cv_cxx_stdcxx_tr1" = yes; then
672 AC_DEFINE(STDCXX_TR1_HEADERS,,[Define if ISO C++ TR1 header files are present. ])
675 </pre><p>An alternative is to check just for specific TR1 includes, such as
<unordered_map
> and
<unordered_set
>.
676 </p><pre class=
"programlisting">
677 # AC_HEADER_TR1_UNORDERED_MAP
678 AC_DEFUN([AC_HEADER_TR1_UNORDERED_MAP], [
679 AC_CACHE_CHECK(for tr1/unordered_map,
680 ac_cv_cxx_tr1_unordered_map,
683 AC_TRY_COMPILE([#include
<tr1/unordered_map
>], [using std::tr1::unordered_map;],
684 ac_cv_cxx_tr1_unordered_map=yes, ac_cv_cxx_tr1_unordered_map=no)
687 if test
"$ac_cv_cxx_tr1_unordered_map" = yes; then
688 AC_DEFINE(HAVE_TR1_UNORDERED_MAP,,[Define if tr1/unordered_map is present. ])
691 </pre><pre class=
"programlisting">
692 # AC_HEADER_TR1_UNORDERED_SET
693 AC_DEFUN([AC_HEADER_TR1_UNORDERED_SET], [
694 AC_CACHE_CHECK(for tr1/unordered_set,
695 ac_cv_cxx_tr1_unordered_set,
698 AC_TRY_COMPILE([#include
<tr1/unordered_set
>], [using std::tr1::unordered_set;],
699 ac_cv_cxx_tr1_unordered_set=yes, ac_cv_cxx_tr1_unordered_set=no)
702 if test
"$ac_cv_cxx_tr1_unordered_set" = yes; then
703 AC_DEFINE(HAVE_TR1_UNORDERED_SET,,[Define if tr1/unordered_set is present. ])
706 </pre></div><div class=
"section" title=
"Support for C++11 dialect."><div class=
"titlepage"><div><div><h4 class=
"title"><a name=
"backwards.third.support_cxx11"></a>
707 Support for C++
11 dialect.
708 </h4></div></div></div><p>Check for baseline language coverage in the compiler for the C++
11 standard.
709 </p><pre class=
"programlisting">
710 # AC_COMPILE_STDCXX_11
711 AC_DEFUN([AC_COMPILE_STDCXX_11], [
712 AC_CACHE_CHECK(if g++ supports C++
11 features without additional flags,
713 ac_cv_cxx_compile_cxx11_native,
717 template
<typename T
>
720 static constexpr T value{ __cplusplus };
723 typedef check
<check
<bool
>> right_angle_brackets;
728 typedef check
<int
> check_type;
730 check_type
&& cr = static_cast
<check_type
&&>(c);
732 static_assert(check_type::value ==
201103L,
"C++11 compiler");],,
733 ac_cv_cxx_compile_cxx11_native=yes, ac_cv_cxx_compile_cxx11_native=no)
737 AC_CACHE_CHECK(if g++ supports C++
11 features with -std=c++
11,
738 ac_cv_cxx_compile_cxx11_cxx,
741 ac_save_CXXFLAGS=
"$CXXFLAGS"
742 CXXFLAGS=
"$CXXFLAGS -std=c++11"
744 template
<typename T
>
747 static constexpr T value{ __cplusplus };
750 typedef check
<check
<bool
>> right_angle_brackets;
755 typedef check
<int
> check_type;
757 check_type
&& cr = static_cast
<check_type
&&>(c);
759 static_assert(check_type::value ==
201103L,
"C++11 compiler");],,
760 ac_cv_cxx_compile_cxx11_cxx=yes, ac_cv_cxx_compile_cxx11_cxx=no)
761 CXXFLAGS=
"$ac_save_CXXFLAGS"
765 AC_CACHE_CHECK(if g++ supports C++
11 features with -std=gnu++
11,
766 ac_cv_cxx_compile_cxx11_gxx,
769 ac_save_CXXFLAGS=
"$CXXFLAGS"
770 CXXFLAGS=
"$CXXFLAGS -std=gnu++11"
772 template
<typename T
>
775 static constexpr T value{ __cplusplus };
778 typedef check
<check
<bool
>> right_angle_brackets;
783 typedef check
<int
> check_type;
785 check_type
&& cr = static_cast
<check_type
&&>(c);
787 static_assert(check_type::value ==
201103L,
"C++11 compiler");],,
788 ac_cv_cxx_compile_cxx11_gxx=yes, ac_cv_cxx_compile_cxx11_gxx=no)
789 CXXFLAGS=
"$ac_save_CXXFLAGS"
793 if test
"$ac_cv_cxx_compile_cxx11_native" = yes ||
794 test
"$ac_cv_cxx_compile_cxx11_cxx" = yes ||
795 test
"$ac_cv_cxx_compile_cxx11_gxx" = yes; then
796 AC_DEFINE(HAVE_STDCXX_11,,[Define if g++ supports C++
11 features. ])
799 </pre><p>Check for library coverage of the C++
2011 standard.
800 (Some library headers are commented out in this check, they are
801 not currently provided by libstdc++).
802 </p><pre class=
"programlisting">
803 # AC_HEADER_STDCXX_11
804 AC_DEFUN([AC_HEADER_STDCXX_11], [
805 AC_CACHE_CHECK(for ISO C++
11 include files,
807 [AC_REQUIRE([AC_COMPILE_STDCXX_11])
810 ac_save_CXXFLAGS=
"$CXXFLAGS"
811 CXXFLAGS=
"$CXXFLAGS -std=gnu++11"
814 #include
<cassert
>
815 #include
<ccomplex
>
816 #include
<cctype
>
817 #include
<cerrno
>
818 #include
<cfenv
>
819 #include
<cfloat
>
820 #include
<cinttypes
>
821 #include
<ciso646
>
822 #include
<climits
>
823 #include
<clocale
>
824 #include
<cmath
>
825 #include
<csetjmp
>
826 #include
<csignal
>
827 #include
<cstdalign
>
828 #include
<cstdarg
>
829 #include
<cstdbool
>
830 #include
<cstddef
>
831 #include
<cstdint
>
832 #include
<cstdio
>
833 #include
<cstdlib
>
834 #include
<cstring
>
835 #include
<ctgmath
>
836 #include
<ctime
>
837 // #include
<cuchar
>
838 #include
<cwchar
>
839 #include
<cwctype
>
841 #include
<algorithm
>
842 #include
<array
>
843 #include
<atomic
>
844 #include
<bitset
>
845 #include
<chrono
>
846 // #include
<codecvt
>
847 #include
<complex
>
848 #include
<condition_variable
>
849 #include
<deque
>
850 #include
<exception
>
851 #include
<forward_list
>
852 #include
<fstream
>
853 #include
<functional
>
854 #include
<future
>
855 #include
<initializer_list
>
856 #include
<iomanip
>
858 #include
<iosfwd
>
859 #include
<iostream
>
860 #include
<istream
>
861 #include
<iterator
>
862 #include
<limits
>
863 #include
<list
>
864 #include
<locale
>
866 #include
<memory
>
867 #include
<mutex
>
869 #include
<numeric
>
870 #include
<ostream
>
871 #include
<queue
>
872 #include
<random
>
873 #include
<ratio
>
874 #include
<regex
>
875 #include
<scoped_allocator
>
877 #include
<sstream
>
878 #include
<stack
>
879 #include
<stdexcept
>
880 #include
<streambuf
>
881 #include
<string
>
882 #include
<system_error
>
883 #include
<thread
>
884 #include
<tuple
>
885 #include
<typeindex
>
886 #include
<typeinfo
>
887 #include
<type_traits
>
888 #include
<unordered_map
>
889 #include
<unordered_set
>
890 #include
<utility
>
891 #include
<valarray
>
892 #include
<vector
>
894 ac_cv_cxx_stdcxx_11=yes, ac_cv_cxx_stdcxx_11=no)
896 CXXFLAGS=
"$ac_save_CXXFLAGS"
898 if test
"$ac_cv_cxx_stdcxx_11" = yes; then
899 AC_DEFINE(STDCXX_11_HEADERS,,[Define if ISO C++
11 header files are present. ])
902 </pre><p>As is the case for TR1 support, these autoconf macros can be made for a finer-grained, per-header-file check. For
903 <code class=
"filename"><unordered_map
></code>
904 </p><pre class=
"programlisting">
905 # AC_HEADER_UNORDERED_MAP
906 AC_DEFUN([AC_HEADER_UNORDERED_MAP], [
907 AC_CACHE_CHECK(for unordered_map,
908 ac_cv_cxx_unordered_map,
909 [AC_REQUIRE([AC_COMPILE_STDCXX_11])
912 ac_save_CXXFLAGS=
"$CXXFLAGS"
913 CXXFLAGS=
"$CXXFLAGS -std=gnu++11"
914 AC_TRY_COMPILE([#include
<unordered_map
>], [using std::unordered_map;],
915 ac_cv_cxx_unordered_map=yes, ac_cv_cxx_unordered_map=no)
916 CXXFLAGS=
"$ac_save_CXXFLAGS"
919 if test
"$ac_cv_cxx_unordered_map" = yes; then
920 AC_DEFINE(HAVE_UNORDERED_MAP,,[Define if unordered_map is present. ])
923 </pre><pre class=
"programlisting">
924 # AC_HEADER_UNORDERED_SET
925 AC_DEFUN([AC_HEADER_UNORDERED_SET], [
926 AC_CACHE_CHECK(for unordered_set,
927 ac_cv_cxx_unordered_set,
928 [AC_REQUIRE([AC_COMPILE_STDCXX_11])
931 ac_save_CXXFLAGS=
"$CXXFLAGS"
932 CXXFLAGS=
"$CXXFLAGS -std=gnu++11"
933 AC_TRY_COMPILE([#include
<unordered_set
>], [using std::unordered_set;],
934 ac_cv_cxx_unordered_set=yes, ac_cv_cxx_unordered_set=no)
935 CXXFLAGS=
"$ac_save_CXXFLAGS"
938 if test
"$ac_cv_cxx_unordered_set" = yes; then
939 AC_DEFINE(HAVE_UNORDERED_SET,,[Define if unordered_set is present. ])
943 Some C++
11 features first appeared in GCC
4.3 and could be enabled by
944 <code class=
"option">-std=c++
0x
</code> and
<code class=
"option">-std=gnu++
0x
</code> for GCC
945 releases which pre-date the
2011 standard. Those C++
11 features and GCC's
946 support for them were still changing until the
2011 standard was finished,
947 but the autoconf checks above could be extended to test for incomplete
948 C++
11 support with
<code class=
"option">-std=c++
0x
</code> and
949 <code class=
"option">-std=gnu++
0x
</code>.
950 </p></div><div class=
"section" title=
"Container::iterator_type is not necessarily Container::value_type*"><div class=
"titlepage"><div><div><h4 class=
"title"><a name=
"backwards.third.iterator_type"></a>
951 <code class=
"code">Container::iterator_type
</code> is not necessarily
<code class=
"code">Container::value_type*
</code>
952 </h4></div></div></div><p>
953 This is a change in behavior from older versions. Now, most
954 <span class=
"type">iterator_type
</span> typedefs in container classes are POD
955 objects, not
<span class=
"type">value_type
</span> pointers.
956 </p></div></div><div class=
"bibliography" title=
"Bibliography"><div class=
"titlepage"><div><div><h3 class=
"title"><a name=
"backwards.biblio"></a>Bibliography
</h3></div></div></div><div class=
"biblioentry" title=
"Migrating to GCC 4.1"><a name=
"id688193"></a><p><span class=
"title"><i>
957 <a class=
"link" href=
"http://www.kegel.com/gcc/gcc4.html" target=
"_top">
960 </i>.
</span><span class=
"author"><span class=
"firstname">Dan
</span> <span class=
"surname">Kegel
</span>.
</span></p></div><div class=
"biblioentry" title=
"Building the Whole Debian Archive with GCC 4.1: A Summary"><a name=
"id688216"></a><p><span class=
"title"><i>
961 <a class=
"link" href=
"http://lists.debian.org/debian-gcc/2006/03/msg00405.html" target=
"_top">
962 Building the Whole Debian Archive with GCC
4.1: A Summary
964 </i>.
</span><span class=
"author"><span class=
"firstname">Martin
</span> <span class=
"surname">Michlmayr
</span>.
</span></p></div><div class=
"biblioentry" title=
"Migration guide for GCC-3.2"><a name=
"id688240"></a><p><span class=
"title"><i>
965 <a class=
"link" href=
"http://annwm.lbl.gov/~leggett/Atlas/gcc-3.2.html" target=
"_top">
966 Migration guide for GCC-
3.2
968 </i>.
</span></p></div></div></div><div class=
"navfooter"><hr><table width=
"100%" summary=
"Navigation footer"><tr><td width=
"40%" align=
"left"><a accesskey=
"p" href=
"api.html">Prev
</a> </td><td width=
"20%" align=
"center"><a accesskey=
"u" href=
"appendix_porting.html">Up
</a></td><td width=
"40%" align=
"right"> <a accesskey=
"n" href=
"appendix_free.html">Next
</a></td></tr><tr><td width=
"40%" align=
"left" valign=
"top">API Evolution and Deprecation History
</td><td width=
"20%" align=
"center"><a accesskey=
"h" href=
"../index.html">Home
</a></td><td width=
"40%" align=
"right" valign=
"top"> Appendix C.
969 Free Software Needs Free Documentation
971 </td></tr></table></div></body></html>