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