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://libsigc.sourceforge.net">
284 libsigc++</ulink></entry>
285 <entry>conservative-impl</entry>
291 <table><title>Notations for categories</title>
296 <entry>mostly fully qualified names and some
297 using-declarations (but not in headers)</entry>
300 <entry>none</entry> <entry>no namespace std at all</entry>
303 <entry>conservative-impl</entry>
305 namespace-handling in macros to support compilers without
306 namespace-support (no libstdc++ used in headers)</entry>
313 As you can see, this currently lacks an example of a project
314 which uses libstdc++-symbols in headers in a back-portable way
315 (except for Gtk--: see the <link linkend="sec-gtkmm-hack"
316 endterm="sec-gtkmm-hack.title">section on the gtkmm-hack</link>).
319 </section> <!-- end of namespace-section -->
321 <section id = "sec-nocreate" label = "2">
322 <title id="sec-nocreate.title">there is no ios::nocreate/ios::noreplace
325 I have seen <command>ios::nocreate</command> being used for
326 input-streams, most probably because the author thought it would be
327 more correct to specify nocreate "explicitly". So you can simply
328 leave it out for input-streams.
331 For output streams, "nocreate" is probably the default, unless you
332 specify <command>std::ios::trunc</command> ? To be safe, you can open
333 the file for reading, check if it has been opened, and then decide
334 whether you want to create/replace or not. To my knowledge, even
335 older implementations support <command>app</command>,
336 <command>ate</command> and <command>trunc</command> (except for
337 <command>app</command> ?).
341 <section id = "sec-stream::attach" label = "3">
342 <title id="sec-stream::attach.title"><command>stream::attach(int
343 fd)</command> is not in the standard any more</title>
345 Phil Edwards <email>pedwards@disaster.jaj.com</email> writes:
346 It was considered and rejected. Not all environments use file
347 descriptors. Of those that do, not all of them use integers to represent
351 When using libstdc++-v3, you can use
353 <funcsynopsisinfo format="linespecific">
354 #include <fstream>
358 <function>basic_filebuf<...>::basic_filebuf<...>
361 <paramdef>__c_file_type* <parameter>file</parameter></paramdef>
362 <paramdef>ios_base::open_mode <parameter>mode</parameter></paramdef>
363 <paramdef>int <parameter>size</parameter></paramdef>
366 but the the signature of this constructor has changed often, and
367 it might change again. For the current state of this, check
368 <ulink url="../ext/howto.html">the howto for extensions</ulink>.
371 For a portable solution (among systems which use
372 filedescriptors), you need to implement a subclass of
373 <command>std::streambuf</command> (or
374 <command>std::basic_streambuf<..></command>) which opens a file
375 given a descriptor, and then pass an instance of this to the
376 stream-constructor. For an example of this, refer to
377 <ulink url="http://www.josuttis.com/cppcode/fdstream.html">fdstream example</ulink>
382 <section id = "sec-headers" label = "4">
383 <title id="sec-headers.title">The new headers</title>
385 All new headers can be seen in this <ulink url="headers_cc.txt">
389 The old C++-headers (iostream.h etc.) are available, but gcc generates
390 a warning that you are using deprecated headers.
393 <section id = "sec-cheaders" label = "4.1">
394 <title id="sec-cheaders.title">New headers replacing C-headers</title>
396 You should not use the C-headers (except for system-level
397 headers) from C++ programs. Instead, you should use a set of
398 headers that are named by prepending 'c' and, as usual,
399 omitting the extension (.h). For example, instead of using
400 <filename class="headerfile"><math.h></filename>, you
401 should use <filename class =
402 "headerfile"><cmath></filename>. In some cases this has
403 the advantage that the C++-header is more standardized than
404 the C-header (i.e. <filename
405 class="headerfile"><ctime></filename> (almost)
406 corresponds to either <filename class =
407 "headerfile"><time.h></filename> or <filename class =
408 "headerfile"><sys/time.h></filename>).
410 The standard specifies that if you include the C-style header
411 (<filename class = "headerfile"><math.h></filename> in
412 this case), the symbols will be available both in the global
413 namespace and in namespace <command>std::</command> (but
414 libstdc++ does not yet have fully compliant headers) On the
415 other hand, if you include only the new header (i.e. <filename
416 class = "headerfile"><cmath></filename>), the symbols
417 will only be defined in namespace <command>std::</command>
418 (and macros will be converted to inline-functions).
421 For more information on this, and for information on how the
422 GNU C++ implementation might reuse ("shadow") the C
423 library-functions, have a look at <ulink
424 url="http://www.cantrip.org/cheaders.html">
425 www.cantrip.org</ulink>.
429 <section id = "sec-fstream-header" label = "4.2">
430 <title id="sec-fstream-header.title">
431 <filename class="headerfile"><fstream></filename> does
432 not define <command>std::cout</command>,
433 <command>std::cin</command> etc.</title>
435 In earlier versions of the standard,
436 <filename class="headerfile"><fstream.h></filename>,
437 <filename class="headerfile"><ostream.h></filename>
438 and <filename class="headerfile"><istream.h></filename>
440 <command>cout</command>, <command>cin</command> and so on. Because
441 of the templatized iostreams in libstdc++-v3, you need to include
442 <filename class = "headerfile"><iostream></filename>
443 explicitly to define these.
448 <section id = "sec-iterators" label = "5">
449 <title id="sec-iterators.title">Iterators</title>
451 The following are not proper uses of iterators, but may be working
452 fixes for existing uses of iterators.
454 <listitem><para>you cannot do
455 <command>ostream::operator<<(iterator)</command> to
456 print the address of the iterator => use
457 <command>operator<< &*iterator</command> instead ?
460 <listitem><para>you cannot clear an iterator's reference
461 (<command>iterator = 0</command>) => use
462 <command>iterator = iterator_type();</command> ?
465 <listitem><para><command>if (iterator)</command> won't work any
466 more => use <command>if (iterator != iterator_type())</command>
473 <section id = "sec-macros" label = "6">
474 <title id="sec-macros.title">
475 Libc-macros (i.e. <command>isspace</command> from
476 <filename class = "headerfile"><cctype></filename>)</title>
478 Glibc 2.0.x and 2.1.x define the
479 <filename class="headerfile"><ctype.h></filename>
480 -functionality as macros (isspace, isalpha etc.). Libstdc++-v3
481 "shadows" these macros as described in the <link
482 linkend="sec-cheaders" endterm="sec-cheaders.title">section about
486 Older implementations of libstdc++ (g++-2 for egcs 1.x and g++-3
487 for gcc 2.95.x), however, keep these functions as macros, and so it
488 is not back-portable to use fully qualified names. For example:
490 #include <cctype>
491 int main() { std::isspace('X'); }
493 will result in something like this (unless using g++-v3):
495 std:: (__ctype_b[(int) ( ( 'X' ) )] & (unsigned short int)
500 One solution I can think of is to test for -v3 using
501 autoconf-macros, and define macros for each of the C-functions
502 (maybe that is possible with one "wrapper" macro as well ?).
505 Another solution which would fix g++ is to tell the user to modify a
506 header-file so that g++-2 (egcs 1.x) and g++-3 (gcc 2.95.x) define a
507 macro which tells <filename
508 class="headerfile"><ctype.h></filename> to define functions
511 // This keeps isalnum, et al from being propagated as macros.
516 [ now include <ctype.h> ]
520 Another problem arises if you put a <command>using namespace
521 std;</command> declaration at the top, and include <filename class
522 = "headerfile"><ctype.h></filename>. This will result in
523 ambiguities between the definitions in the global namespace
524 (<filename class = "headerfile"><ctype.h></filename>) and the
525 definitions in namespace <command>std::</command>
526 (<command><cctype></command>).
529 The solution to this problem was posted to the libstdc++-v3
531 Benjamin Kosnik <email>bkoz@redhat.com</email> writes:
533 --enable-cshadow-headers is currently broken. As a result, shadow
534 headers are not being searched....
536 This is now outdated, but gcc 3.0 still does not have fully
537 compliant "shadow headers".
541 <section id="sec-stream-state" label="7">
542 <title id="sec-stream-state.title">State of streams</title>
544 At least some older implementations don't have
545 <command>std::ios_base</command>, so you should use
546 <command>std::ios::badbit</command>, <command>std::ios::failbit</command>
547 and <command>std::ios::eofbit</command> and
548 <command>std::ios::goodbit</command>.
552 <section id="sec-vector-at" label="8">
553 <title>vector::at is missing (i.e. gcc 2.95.x)</title>
555 One solution is to add an autoconf-test for this:
557 AC_MSG_CHECKING(for container::at)
560 #include <vector>
561 #include <deque>
562 #include <string>
567 deque<int> test_deque(3);
569 vector<int> test_vector(2);
571 string test_string("test_string");
575 AC_DEFINE(HAVE_CONTAINER_AT)],
578 If you are using other (non-GNU) compilers it might be a good idea
579 to check for <command>string::at</command> separately.
583 <section id="sec-eof" label="9">
584 <title>Using std::char_traits<char>::eof()</title>
587 #ifdef HAVE_CHAR_TRAITS
588 #define CPP_EOF std::char_traits<char>::eof()
596 <section id="sec-string-clear" label="10">
597 <title>Using string::clear()/string::erase()</title>
599 There are two functions for deleting the contents of a string:
600 <command>clear</command> and <command>erase</command> (the latter
604 clear() { _M_mutate(0, this->size(), 0); }
608 erase(size_type __pos = 0, size_type __n = npos)
610 return this->replace(_M_check(__pos), _M_fold(__pos, __n),
611 _M_data(), _M_data());
614 The implementation of <command>erase</command> seems to be more
615 complicated (from libstdc++-v3), but <command>clear</command> is not
616 implemented in gcc 2.95.x's libstdc++, so you should use
617 <command>erase</command> (which is probably faster than
618 <command>operator=(charT*)</command>).
622 <section id="sec-scan-form" label="11">
623 <title>GNU Extensions ostream::form and istream::scan</title>
625 These are not supported any more - use
626 <link linkend="sec-stringstream" endterm="sec-stringstream.title">
627 stringstreams</link> instead.
631 <section id="sec-stringstream" label="12">
632 <title>Using stringstreams</title>
634 Libstdc++-v3 provides the new
635 <command>i/ostringstream</command>-classes, (<filename
636 class="headerfile"><sstream></filename>), but for compatibility
637 with older implementations you still have to use
638 <command>i/ostrstream</command> (<filename
639 class="headerfile"><strstream></filename>):
642 #include <sstream>
644 #include <strstream>
648 <listitem><para> <command>strstream</command> is considered to be
652 <listitem><para> <command>strstream</command> is limited to
653 <command>char</command>
656 <listitem><para> with <command>ostringstream</command> you don't
657 have to take care of terminating the string or freeing its
661 <listitem><para> <command>istringstream</command> can be re-filled
662 (clear(); str(input);)
668 You can then use output-stringstreams like this:
671 std::ostringstream oss;
675 oss << "Name=" << m_name << ", number=" << m_number << std::endl;
678 oss << std::ends; // terminate the char*-string
680 // str() returns char* for ostrstream and a string for ostringstream
681 // this also causes ostrstream to think that the buffer's memory
683 m_label.set_text(oss.str());
685 // let the ostrstream take care of freeing the memory
691 Input-stringstreams can be used similarly:
696 std::istringstream iss(input);
698 std::istrstream iss(input.c_str());
703 One (the only?) restriction is that an istrstream cannot be re-filled:
705 std::istringstream iss(numerator);
707 // this is not possible with istrstream
709 iss.str(denominator);
712 If you don't care about speed, you can put these conversions in
715 template <class X>
716 void fromString(const string& input, X& any)
719 std::istringstream iss(input);
721 std::istrstream iss(input.c_str());
726 throw runtime_error(..)
730 Another example of using stringstreams is in <ulink
731 url="../21_strings/howto.html">this howto</ulink>.
734 I have read the Josuttis book on Standard C++, so some information
735 comes from there. Additionally, there is information in
736 "info iostream", which covers the old implementation that gcc 2.95.x
741 <section id = "sec-about" label="13">
742 <title id="sec-about.title">About...</title>
744 Please send any experience, additions, corrections or questions to
745 <ulink url = "mailto:fnatter@gmx.net">fnatter@gmx.net</ulink> or for
746 discussion to the libstdc++-v3-mailing-list.
752 <!-- this is now obsolete, since the nwalsh-stylesheet generates an index
756 <link linkend = "sec-nsstd" endterm = "sec-nsstd.title"/>
759 <link linkend = "sec-nocreate" endterm = "sec-nocreate.title"/>
762 <link linkend = "sec-stream::attach"
763 endterm = "sec-stream::attach.title"/>
766 <link linkend = "sec-headers" endterm = "sec-headers.title"/>
769 <link linkend = "sec-iterators" endterm = "sec-iterators.title"/>
772 <link linkend = "sec-macros" endterm = "sec-macros.title"/>
775 <link linkend = "sec-about" endterm = "sec-about.title"/>
783 compile-command: "xsltproc -o porting-howto.html docbook-xslt/docbook/html/docbook.xsl porting-howto.xml"