1 <?xml version="1.0" encoding="ISO-8859-1"?>
3 <!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.1//EN"
4 "http://www.oasis-open.org/docbook/xml/4.0/docbookx.dtd">
6 <?xml-stylesheet type="text/xsl" href="docbook-xslt/docbook/html/docbook.xsl"?>
9 This is written using docbook 4.1 xml. HTML is generated using
10 the xslt-stylesheets from http://www.nwalsh.com.
12 xsltproc is an xslt-processor included in libxslt:
13 (http://xmlsoft.org/XSLT/ or here:
14 ftp://ftp.gnome.org/pub/GNOME/unstable/sources/libxslt/)
15 (it requires libxml2: http://xmlsoft.org
16 or here: ftp://ftp.gnome.org/pub/GNOME/stable/sources/libxml/)
18 You can find the latest version of this document here:
19 http://gcc.gnu.org/onlinedocs/libstdc++/17_intro/porting-howto(.html|.xml)
23 o remove //@label: use automatic numbering
24 o make this work: <link linkend="sec-gtkmm-hack" endterm="sec-gtkmm-hack.title"/>.
25 o clean up the section-numbering
28 <article class = "whitepaper" id = "libstdc++-porting-howto" lang = "en">
30 <title>Libstdc++-porting-howto</title>
32 <firstname>Felix</firstname>
33 <surname>Natter</surname>
36 <email>fnatter@gmx.net</email>
40 <revnumber>0.5</revnumber>
41 <date>Thu Jun 1 13:06:50 2000</date>
42 <authorinitials>fnatter</authorinitials>
43 <revremark>First docbook-version.</revremark>
46 <revnumber>0.8</revnumber>
47 <date>Sun Jul 30 20:28:40 2000</date>
48 <authorinitials>fnatter</authorinitials>
49 <revremark>First released version using docbook-xml
50 + second upload to libstdc++-page.
54 <revnumber>0.9</revnumber>
55 <date>Wed Sep 6 02:59:32 2000</date>
56 <authorinitials>fnatter</authorinitials>
57 <revremark>5 new sections.</revremark>
60 <revnumber>0.9.1</revnumber>
61 <date>Sat Sep 23 14:20:15 2000</date>
62 <authorinitials>fnatter</authorinitials>
63 <revremark>added information about why file-descriptors are not in the
67 <revnumber>0.9.2</revnumber>
68 <date>Tue Jun 5 20:07:49 2001</date>
69 <authorinitials>fnatter</authorinitials>
71 a fix, added hint on increased portability of C-shadow-headers,
72 added autoconf-test HAVE_CONTAINER_AT
76 <revnumber>0.9.3</revnumber>
77 <date>Fri Jun 29 16:15:56 2001</date>
78 <authorinitials>fnatter</authorinitials>
80 changed signature of nonstandard filebuf-constructor and
81 update the section on filebuf::attach to point to ../ext/howto.html,
82 added link to ../21/strings/howto.html
83 in sec-stringstream, changed <link>-tags to have content
84 (so that these links work),
85 replace "user-space" by "global namespace"
86 add note about gcc 3.0 and shadow-headers
87 add section about ostream::form and istream::scan
88 sec-vector-at: remove hint to modify headers
89 fix spelling error in sec-stringstream
93 <revnumber>0.9.4</revnumber>
94 <date>Mon Nov 5 17:01:04 2001</date>
95 <authorinitials>fnatter</authorinitials>
97 rewrite section 1.1.3 because of gnu.gcc.help-post by
102 <legalnotice><title>Legal Notice</title>
104 This document can be distributed under the FDL
105 (<ulink url = "http://www.gnu.org">www.gnu.org</ulink>)
109 <pubdate>Tue Jun 5 20:07:49 2001</pubdate>
112 Some notes on porting applications from libstdc++-2.90 (or earlier
113 versions) to libstdc++-v3. Not speaking in terms of the GNU libstdc++
114 implementations, this means porting from earlier versions of the
115 C++-Standard to ISO 14882.
121 In the following, when I say portable, I will refer to "portable among ISO
122 14882-implementations". On the other hand, if I say "backportable" or
123 "conservative", I am talking about "compiles with older
124 libstdc++-implementations".
127 <section id="sec-nsstd" label="1"><title>Namespace std::</title>
129 The latest C++-standard (ISO-14882) requires that the standard
130 C++-library is defined in namespace std::. Thus, in order to use
131 classes from the standard C++-library, you can do one of three
135 <listitem><para>wrap your code in <command>namespace std {
136 ... }</command> => This is not an option because only symbols
137 from the standard c++-library are defined in namespace std::.
140 <listitem><para>put a kind of
141 <emphasis>using-declaration</emphasis> in your source (either
142 <command>using namespace std;</command> or i.e. <command>using
143 std::string;</command>) => works well for source-files, but
144 cannot be used in header-files.
147 <listitem><para>use a <emphasis>fully qualified name</emphasis> for
148 each libstdc++-symbol (i.e. <command>std::string</command>,
149 <command>std::cout</command>) => can always be used
155 Because there are many compilers which still use an implementation
156 that does not have the standard C++-library in namespace
157 <command>std::</command>, some care is required to support these as
162 Namespace back-portability-issues are generally not a problem with
163 g++, because versions of g++ that do not have libstdc++ in
164 <command>std::</command> use <command>-fno-honor-std</command>
165 (ignore <command>std::</command>, <command>:: = std::</command>) by
166 default. That is, the responsibility for enabling or disabling
167 <command>std::</command> is on the user; the maintainer does not have
168 to care about it. This probably applies to some other compilers as
172 The following sections list some possible solutions to support compilers
173 that cannot ignore std::.
176 <section id = "sec-gtkmm-hack" label = "1.1">
177 <title id="sec-gtkmm-hack.title">Using <emphasis>namespace
178 composition</emphasis> if the project uses a separate
181 <ulink url = "http://gtkmm.sourceforge.net">Gtk--</ulink> defines
182 most of its classes in namespace Gtk::. Thus, it was possible to
183 adapt Gtk-- to namespace std:: by using a C++-feature called
184 <emphasis>namespace composition</emphasis>. This is what happens if
185 you put a <emphasis>using</emphasis>-declaration into a
186 namespace-definition: the imported symbol(s) gets imported into the
187 currently active namespace(s). For example:
194 In this example, <command>std::string</command> gets imported into
195 namespace Gtk::. The result is that you don't have to use
196 <command>std::string</command> in this header, but still
197 <command>std::string</command> does not get imported into
198 the global namespace (::) unless the user does
199 <command>using namespace Gtk;</command> (which is not recommended
200 practice for Gtk--, so it is not a problem). Additionally, the
201 <command>using</command>-declarations are wrapped in macros that
202 are set based on autoconf-tests to either "" or i.e. <command>using
203 std::string;</command> (depending on whether the system has
204 libstdc++ in <command>std::</command> or not). (ideas from
205 <email>llewelly@dbritsch.dsl.xmission.com</email>, Karl Nelson
206 <email>kenelson@ece.ucdavis.edu</email>)
210 <section id = "sec-emptyns" label = "1.2">
211 <title id="sec-emptyns.title">Defining an empty namespace std</title>
213 By defining an (empty) namespace <command>std::</command> before
214 using it, you avoid getting errors on systems where no part of the
215 library is in namespace std:
223 <section id = "sec-avoidfqn" label = "1.3">
224 <title id="sec-avoidfqn.title">Avoid to use fully qualified names
225 (i.e. std::string)</title>
227 If some compilers complain about <command>using
228 std::string;</command>, and if the "hack" for gtk-- mentioned above
229 does not work, then I see two solutions:
233 Define <command>std::</command> as a macro if the compiler
234 doesn't know about <command>std::</command>.
240 (thanks to Juergen Heinzl who posted this solution on
245 Define a macro <symbol>NS_STD</symbol>, which is defined to
247 based on an autoconf-test. Then you should be able to use
248 <command>NS_STD::string</command>, which will evaluate to
249 <command>::string</command> ("string in the global namespace") on
250 systems that do not put string in std::. (This is untested)
257 <section id = "sec-osprojects" label = "1.4">
258 <title id="sec-osprojects.title">How some open-source-projects deal
261 This information was gathered around May 2000. It may not be correct
262 by the time you read this.
264 <table><title>Namespace std:: in Open-Source programs</title>
268 <entry><ulink url = "http://www.clanlib.org">clanlib</ulink>
273 <entry><ulink url = "http://pingus.seul.org">pingus</ulink>
278 <entry><ulink url = "http://www.mozilla.org">mozilla</ulink>
283 <entry><ulink url = "http://www.mnemonic.org">mnemonic</ulink>
284 </entry> <entry>none</entry>
287 <entry><ulink url = "http://libsigc.sourceforge.net">
288 libsigc++</ulink></entry>
289 <entry>conservative-impl</entry>
295 <table><title>Notations for categories</title>
300 <entry>mostly fully qualified names and some
301 using-declarations (but not in headers)</entry>
304 <entry>none</entry> <entry>no namespace std at all</entry>
307 <entry>conservative-impl</entry>
309 namespace-handling in macros to support compilers without
310 namespace-support (no libstdc++ used in headers)</entry>
317 As you can see, this currently lacks an example of a project
318 which uses libstdc++-symbols in headers in a back-portable way
319 (except for Gtk--: see the <link linkend="sec-gtkmm-hack"
320 endterm="sec-gtkmm-hack.title">section on the gtkmm-hack</link>).
323 </section> <!-- end of namespace-section -->
325 <section id = "sec-nocreate" label = "2">
326 <title id="sec-nocreate.title">there is no ios::nocreate/ios::noreplace
329 I have seen <command>ios::nocreate</command> being used for
330 input-streams, most probably because the author thought it would be
331 more correct to specify nocreate "explicitly". So you can simply
332 leave it out for input-streams.
335 For output streams, "nocreate" is probably the default, unless you
336 specify <command>std::ios::trunc</command> ? To be safe, you can open
337 the file for reading, check if it has been opened, and then decide
338 whether you want to create/replace or not. To my knowledge, even
339 older implementations support <command>app</command>,
340 <command>ate</command> and <command>trunc</command> (except for
341 <command>app</command> ?).
345 <section id = "sec-stream::attach" label = "3">
346 <title id="sec-stream::attach.title"><command>stream::attach(int
347 fd)</command> is not in the standard any more</title>
349 Phil Edwards <email>pedwards@disaster.jaj.com</email> writes:
350 It was considered and rejected. Not all environments use file
351 descriptors. Of those that do, not all of them use integers to represent
355 When using libstdc++-v3, you can use
357 <funcsynopsisinfo format="linespecific">
358 #include <fstream>
362 <function>basic_filebuf<...>::basic_filebuf<...>
365 <paramdef>__c_file_type* <parameter>file</parameter></paramdef>
366 <paramdef>ios_base::open_mode <parameter>mode</parameter></paramdef>
367 <paramdef>int <parameter>size</parameter></paramdef>
370 but the the signature of this constructor has changed often, and
371 it might change again. For the current state of this, check
372 <ulink url="../ext/howto.html">the howto for extensions</ulink>.
375 For a portable solution (among systems which use
376 filedescriptors), you need to implement a subclass of
377 <command>std::streambuf</command> (or
378 <command>std::basic_streambuf<..></command>) which opens a file
379 given a descriptor, and then pass an instance of this to the
380 stream-constructor. For an example of this, refer to
381 <ulink url="http://www.josuttis.com/cppcode/fdstream.html">fdstream example</ulink>
386 <section id = "sec-headers" label = "4">
387 <title id="sec-headers.title">The new headers</title>
389 All new headers can be seen in this <ulink url="headers_cc.txt">
393 The old C++-headers (iostream.h etc.) are available, but gcc generates
394 a warning that you are using deprecated headers.
397 <section id = "sec-cheaders" label = "4.1">
398 <title id="sec-cheaders.title">New headers replacing C-headers</title>
400 You should not use the C-headers (except for system-level
401 headers) from C++ programs. Instead, you should use a set of
402 headers that are named by prepending 'c' and, as usual,
403 omitting the extension (.h). For example, instead of using
404 <filename class="headerfile"><math.h></filename>, you
405 should use <filename class =
406 "headerfile"><cmath></filename>. In some cases this has
407 the advantage that the C++-header is more standardized than
408 the C-header (i.e. <filename
409 class="headerfile"><ctime></filename> (almost)
410 corresponds to either <filename class =
411 "headerfile"><time.h></filename> or <filename class =
412 "headerfile"><sys/time.h></filename>).
414 The standard specifies that if you include the C-style header
415 (<filename class = "headerfile"><math.h></filename> in
416 this case), the symbols will be available both in the global
417 namespace and in namespace <command>std::</command> (but
418 libstdc++ does not yet have fully compliant headers) On the
419 other hand, if you include only the new header (i.e. <filename
420 class = "headerfile"><cmath></filename>), the symbols
421 will only be defined in namespace <command>std::</command>
422 (and macros will be converted to inline-functions).
425 For more information on this, and for information on how the
426 GNU C++ implementation might reuse ("shadow") the C
427 library-functions, have a look at <ulink
428 url="http://www.cantrip.org/cheaders.html">
429 www.cantrip.org</ulink>.
433 <section id = "sec-fstream-header" label = "4.2">
434 <title id="sec-fstream-header.title">
435 <filename class="headerfile"><fstream></filename> does
436 not define <command>std::cout</command>,
437 <command>std::cin</command> etc.</title>
439 In earlier versions of the standard,
440 <filename class="headerfile"><fstream.h></filename>,
441 <filename class="headerfile"><ostream.h></filename>
442 and <filename class="headerfile"><istream.h></filename>
444 <command>cout</command>, <command>cin</command> and so on. Because
445 of the templatized iostreams in libstdc++-v3, you need to include
446 <filename class = "headerfile"><iostream></filename>
447 explicitly to define these.
452 <section id = "sec-iterators" label = "5">
453 <title id="sec-iterators.title">Iterators</title>
455 The following are not proper uses of iterators, but may be working
456 fixes for existing uses of iterators.
458 <listitem><para>you cannot do
459 <command>ostream::operator<<(iterator)</command> to
460 print the address of the iterator => use
461 <command>operator<< &*iterator</command> instead ?
464 <listitem><para>you cannot clear an iterator's reference
465 (<command>iterator = 0</command>) => use
466 <command>iterator = iterator_type();</command> ?
469 <listitem><para><command>if (iterator)</command> won't work any
470 more => use <command>if (iterator != iterator_type())</command>
477 <section id = "sec-macros" label = "6">
478 <title id="sec-macros.title">
479 Libc-macros (i.e. <command>isspace</command> from
480 <filename class = "headerfile"><cctype></filename>)</title>
482 Glibc 2.0.x and 2.1.x define the
483 <filename class="headerfile"><ctype.h></filename>
484 -functionality as macros (isspace, isalpha etc.). Libstdc++-v3
485 "shadows" these macros as described in the <link
486 linkend="sec-cheaders" endterm="sec-cheaders.title">section about
490 Older implementations of libstdc++ (g++-2 for egcs 1.x and g++-3
491 for gcc 2.95.x), however, keep these functions as macros, and so it
492 is not back-portable to use fully qualified names. For example:
494 #include <cctype>
495 int main() { std::isspace('X'); }
497 will result in something like this (unless using g++-v3):
499 std:: (__ctype_b[(int) ( ( 'X' ) )] & (unsigned short int)
504 One solution I can think of is to test for -v3 using
505 autoconf-macros, and define macros for each of the C-functions
506 (maybe that is possible with one "wrapper" macro as well ?).
509 Another solution which would fix g++ is to tell the user to modify a
510 header-file so that g++-2 (egcs 1.x) and g++-3 (gcc 2.95.x) define a
511 macro which tells <filename
512 class="headerfile"><ctype.h></filename> to define functions
515 // This keeps isalnum, et al from being propagated as macros.
520 [ now include <ctype.h> ]
524 Another problem arises if you put a <command>using namespace
525 std;</command> declaration at the top, and include <filename class
526 = "headerfile"><ctype.h></filename>. This will result in
527 ambiguities between the definitions in the global namespace
528 (<filename class = "headerfile"><ctype.h></filename>) and the
529 definitions in namespace <command>std::</command>
530 (<command><cctype></command>).
533 The solution to this problem was posted to the libstdc++-v3
535 Benjamin Kosnik <email>bkoz@redhat.com</email> writes:
537 --enable-cshadow-headers is currently broken. As a result, shadow
538 headers are not being searched....
540 This is now outdated, but gcc 3.0 still does not have fully
541 compliant "shadow headers".
545 <section id="sec-stream-state" label="7">
546 <title id="sec-stream-state.title">State of streams</title>
548 At least some older implementations don't have
549 <command>std::ios_base</command>, so you should use
550 <command>std::ios::badbit</command>, <command>std::ios::failbit</command>
551 and <command>std::ios::eofbit</command> and
552 <command>std::ios::goodbit</command>.
556 <section id="sec-vector-at" label="8">
557 <title>vector::at is missing (i.e. gcc 2.95.x)</title>
559 One solution is to add an autoconf-test for this:
561 AC_MSG_CHECKING(for container::at)
564 #include <vector>
565 #include <deque>
566 #include <string>
571 deque<int> test_deque(3);
573 vector<int> test_vector(2);
575 string test_string("test_string");
579 AC_DEFINE(HAVE_CONTAINER_AT)],
582 If you are using other (non-GNU) compilers it might be a good idea
583 to check for <command>string::at</command> separately.
587 <section id="sec-eof" label="9">
588 <title>Using std::char_traits<char>::eof()</title>
591 #ifdef HAVE_CHAR_TRAITS
592 #define CPP_EOF std::char_traits<char>::eof()
600 <section id="sec-string-clear" label="10">
601 <title>Using string::clear()/string::erase()</title>
603 There are two functions for deleting the contents of a string:
604 <command>clear</command> and <command>erase</command> (the latter
608 clear() { _M_mutate(0, this->size(), 0); }
612 erase(size_type __pos = 0, size_type __n = npos)
614 return this->replace(_M_check(__pos), _M_fold(__pos, __n),
615 _M_data(), _M_data());
618 The implementation of <command>erase</command> seems to be more
619 complicated (from libstdc++-v3), but <command>clear</command> is not
620 implemented in gcc 2.95.x's libstdc++, so you should use
621 <command>erase</command> (which is probably faster than
622 <command>operator=(charT*)</command>).
626 <section id="sec-scan-form" label="11">
627 <title>GNU Extensions ostream::form and istream::scan</title>
629 These are not supported any more - use
630 <link linkend="sec-stringstream" endterm="sec-stringstream.title">
631 stringstreams</link> instead.
635 <section id="sec-stringstream" label="12">
636 <title>Using stringstreams</title>
638 Libstdc++-v3 provides the new
639 <command>i/ostringstream</command>-classes, (<filename
640 class="headerfile"><sstream></filename>), but for compatibility
641 with older implementations you still have to use
642 <command>i/ostrstream</command> (<filename
643 class="headerfile"><strstream></filename>):
646 #include <sstream>
648 #include <strstream>
652 <listitem><para> <command>strstream</command> is considered to be
656 <listitem><para> <command>strstream</command> is limited to
657 <command>char</command>
660 <listitem><para> with <command>ostringstream</command> you don't
661 have to take care of terminating the string or freeing its
665 <listitem><para> <command>istringstream</command> can be re-filled
666 (clear(); str(input);)
672 You can then use output-stringstreams like this:
675 std::ostringstream oss;
679 oss << "Name=" << m_name << ", number=" << m_number << std::endl;
682 oss << std::ends; // terminate the char*-string
684 // str() returns char* for ostrstream and a string for ostringstream
685 // this also causes ostrstream to think that the buffer's memory
687 m_label.set_text(oss.str());
689 // let the ostrstream take care of freeing the memory
695 Input-stringstreams can be used similarly:
700 std::istringstream iss(input);
702 std::istrstream iss(input.c_str());
707 One (the only?) restriction is that an istrstream cannot be re-filled:
709 std::istringstream iss(numerator);
711 // this is not possible with istrstream
713 iss.str(denominator);
716 If you don't care about speed, you can put these conversions in
719 template <class X>
720 void fromString(const string& input, X& any)
723 std::istringstream iss(input);
725 std::istrstream iss(input.c_str());
730 throw runtime_error(..)
734 Another example of using stringstreams is in <ulink
735 url="../21_strings/howto.html">this howto</ulink>.
738 I have read the Josuttis book on Standard C++, so some information
739 comes from there. Additionally, there is information in
740 "info iostream", which covers the old implementation that gcc 2.95.x
745 <section id = "sec-about" label="13">
746 <title id="sec-about.title">About...</title>
748 Please send any experience, additions, corrections or questions to
749 <ulink url = "mailto:fnatter@gmx.net">fnatter@gmx.net</ulink> or for
750 discussion to the libstdc++-v3-mailing-list.
756 <!-- this is now obsolete, since the nwalsh-stylesheet generates an index
760 <link linkend = "sec-nsstd" endterm = "sec-nsstd.title"/>
763 <link linkend = "sec-nocreate" endterm = "sec-nocreate.title"/>
766 <link linkend = "sec-stream::attach"
767 endterm = "sec-stream::attach.title"/>
770 <link linkend = "sec-headers" endterm = "sec-headers.title"/>
773 <link linkend = "sec-iterators" endterm = "sec-iterators.title"/>
776 <link linkend = "sec-macros" endterm = "sec-macros.title"/>
779 <link linkend = "sec-about" endterm = "sec-about.title"/>
787 compile-command: "xsltproc -o porting-howto.html docbook-xslt/docbook/html/docbook.xsl porting-howto.xml"