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