OpenCores
URL https://opencores.org/ocsvn/scarts/scarts/trunk

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-gcc/] [gcc-4.1.1/] [libstdc++-v3/] [docs/] [html/] [17_intro/] [porting-howto.xml] - Blame information for rev 20

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 20 jlechner
2
 
3
4
                         "http://www.oasis-open.org/docbook/xml/4.0/docbookx.dtd">
5
 
6
7
 
8
21
 
22
27
 
28
29
  
30
    Libstdc++-porting-howto
31
    
32
      Felix
33
      Natter
34
    
35
    
36
      fnatter@gmx.net
37
    
38
    
39
      
40
        0.5
41
        Thu Jun  1 13:06:50 2000
42
        fnatter
43
        First docbook-version.
44
      
45
      
46
        0.8
47
        Sun Jul 30 20:28:40 2000
48
        fnatter
49
        First released version using docbook-xml
50
          + second upload to libstdc++-page.
51
        
52
      
53
      
54
        0.9
55
        Wed Sep  6 02:59:32 2000
56
        fnatter
57
        5 new sections.
58
      
59
      
60
        0.9.1
61
        Sat Sep 23 14:20:15 2000
62
        fnatter
63
        added information about why file-descriptors are not in the
64
          standard
65
      
66
      
67
        0.9.2
68
        Tue Jun  5 20:07:49 2001
69
        fnatter
70
        
71
          a fix, added hint on increased portability of C-shadow-headers,
72
          added autoconf-test HAVE_CONTAINER_AT
73
        
74
      
75
      
76
        0.9.3
77
        Fri Jun 29 16:15:56 2001
78
        fnatter
79
        
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
90
        
91
      
92
      
93
        0.9.4
94
        Mon Nov  5 17:01:04 2001
95
        fnatter
96
        
97
          rewrite section 1.1.3 because of gnu.gcc.help-post by
98
          Juergen Heinzl
99
        
100
      
101
    
102
    Legal Notice
103
      
104
        This document can be distributed under the FDL
105
        (www.gnu.org)
106
      
107
    
108
 
109
    Tue Jun  5 20:07:49 2001
110
    
111
      
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.
116
      
117
    
118
  
119
 
120
  
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".
125
  
126
 
127
  
Namespace std::
128
    
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
132
      things:
133
      
134
 
135
        wrap your code in namespace std {
136
              ... } => This is not an option because only symbols
137
            from the standard c++-library are defined in namespace std::.
138
          
139
 
140
        put a kind of
141
            using-declaration in your source (either
142
            using namespace std; or i.e. using
143
              std::string;) => works well for source-files, but
144
            cannot be used in header-files.
145
          
146
 
147
        use a fully qualified name for
148
            each libstdc++-symbol (i.e. std::string,
149
            std::cout) => can always be used
150
          
151
      
152
    
153
 
154
    
155
      Because there are many compilers which still use an implementation
156
      that does not have the standard C++-library in namespace
157
      std::, some care is required to support these as
158
      well.
159
    
160
 
161
    
162
      Namespace back-portability-issues are generally not a problem with
163
      g++, because versions of g++ that do not have libstdc++ in
164
      std:: use -fno-honor-std
165
      (ignore std::, :: = std::) by
166
      default. That is, the responsibility for enabling or disabling
167
      std:: is on the user; the maintainer does not have
168
      to care about it. This probably applies to some other compilers as
169
      well.
170
    
171
    
172
      The following sections list some possible solutions to support compilers
173
      that cannot ignore std::.
174
    
175
 
176
    
177
      Using <emphasis>namespace</code></pre></td>
      </tr>
      <tr valign="middle">
         <td>178</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>          composition</emphasis> if the project uses a separate</code></pre></td>
      </tr>
      <tr valign="middle">
         <td>179</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>        namespace
180
      
181
        Gtk-- 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
        namespace composition. This is what happens if
185
        you put a using-declaration into a
186
        namespace-definition: the imported symbol(s) gets imported into the
187
        currently active namespace(s). For example:
188
        
189
          namespace Gtk {
190
          using std::string;
191
          class Window { ... }
192
          }
193
        
194
        In this example, std::string gets imported into
195
        namespace Gtk::.  The result is that you don't have to use
196
        std::string in this header, but still
197
        std::string does not get imported into
198
        the global namespace (::) unless the user does
199
        using namespace Gtk; (which is not recommended
200
        practice for Gtk--, so it is not a problem).  Additionally, the
201
        using-declarations are wrapped in macros that
202
        are set based on autoconf-tests to either "" or i.e. using
203
          std::string; (depending on whether the system has
204
        libstdc++ in std:: or not).  (ideas from
205
        llewelly@dbritsch.dsl.xmission.com, Karl Nelson
206
        kenelson@ece.ucdavis.edu)
207
      
208
    
209
 
210
    
211
      Defining an empty namespace std
212
      
213
        By defining an (empty) namespace std:: before
214
        using it, you avoid getting errors on systems where no part of the
215
        library is in namespace std:
216
        
217
          namespace std { }
218
          using namespace std;
219
        
220
      
221
    
222
 
223
    
224
      Avoid to use fully qualified names</code></pre></td>
      </tr>
      <tr valign="middle">
         <td>225</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>        (i.e. std::string)
226
      
227
        If some compilers complain about using
228
          std::string;, and if the "hack" for gtk-- mentioned above
229
        does not work, then I see two solutions:
230
 
231
        
232
          
233
              Define std:: as a macro if the compiler
234
              doesn't know about std::.
235
              
236
                #ifdef OLD_COMPILER
237
                #define std
238
                #endif
239
              
240
              (thanks to Juergen Heinzl who posted this solution on
241
              gnu.gcc.help)
242
            
243
 
244
          
245
              Define a macro NS_STD, which is defined to
246
              either "" or "std"
247
              based on an autoconf-test. Then you should be able to use
248
              NS_STD::string, which will evaluate to
249
              ::string ("string in the global namespace") on
250
              systems that do not put string in std::.  (This is untested)
251
            
252
        
253
 
254
      
255
    
256
 
257
    
258
      How some open-source-projects deal</code></pre></td>
      </tr>
      <tr valign="middle">
         <td>259</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>        with this
260
      
261
        This information was gathered around May 2000. It may not be correct
262
        by the time you read this.
263
      
264
      Namespace std:: in Open-Source programs
265
        
266
          
267
            
268
              clanlib
269
              
270
              usual
271
            
272
            
273
              pingus
274
              
275
              usual
276
            
277
            
278
              mozilla
279
              
280
              usual
281
            
282
            
283
              
284
                  libsigc++
285
              conservative-impl
286
            
287
          
288
        
289
      
290
 
291
      Notations for categories
292
        
293
          
294
            
295
              usual
296
              mostly fully qualified names and some
297
                using-declarations (but not in headers)
298
            
299
            
300
              none no namespace std at all
301
            
302
            
303
              conservative-impl
304
              wrap all
305
                namespace-handling in macros to support compilers without
306
                namespace-support (no libstdc++ used in headers)
307
            
308
          
309
        
310
      
311
 
312
      
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 
316
          endterm="sec-gtkmm-hack.title">section on the gtkmm-hack).
317
      
318
    
319
   
320
 
321
  
322
    there is no ios::nocreate/ios::noreplace</code></pre></td>
      </tr>
      <tr valign="middle">
         <td>323</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>      in ISO 14882
324
    
325
      I have seen ios::nocreate 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.
329
    
330
    
331
      For output streams, "nocreate" is probably the default, unless you
332
      specify std::ios::trunc ? 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 app,
336
      ate and trunc (except for
337
      app ?).
338
    
339
  
340
 
341
  
342
    <command>stream::attach(int</code></pre></td>
      </tr>
      <tr valign="middle">
         <td>343</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>        fd)</command> is not in the standard any more
344
    
345
      Phil Edwards pedwards@disaster.jaj.com 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
348
      them.
349
    
350
    
351
      When using libstdc++-v3, you can use
352
      
353
        
354
          #include <fstream>
355
        
356
        
357
          
358
            basic_filebuf<...>::basic_filebuf<...>
359
            
360
          
361
          __c_file_type* file
362
          ios_base::open_mode mode
363
          int size
364
        
365
      
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
      the howto for extensions.
369
    
370
    
371
      For a portable solution (among systems which use
372
      filedescriptors), you need to implement a subclass of
373
      std::streambuf (or
374
      std::basic_streambuf<..>) 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
      fdstream example
378
      by Nicolai Josuttis.
379
    
380
  
381
 
382
  
383
    The new headers
384
    
385
      All new headers can be seen in this 
386
        source-code.
387
    
388
    
389
      The old C++-headers (iostream.h etc.) are available, but gcc generates
390
      a warning that you are using deprecated headers.
391
    
392
 
393
    
394
      New headers replacing C-headers
395
      
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
        <math.h>, you
401
        should use 
402
          "headerfile"><cmath>. In some cases this has
403
        the advantage that the C++-header is more standardized than
404
        the C-header (i.e. 
405
          class="headerfile"><ctime> (almost)
406
        corresponds to either 
407
          "headerfile"><time.h> or 
408
          "headerfile"><sys/time.h>).
409
 
410
        The standard specifies that if you include the C-style header
411
        (<math.h> in
412
        this case), the symbols will be available both in the global
413
        namespace and in namespace std:: (but
414
        libstdc++ does not yet have fully compliant headers) On the
415
        other hand, if you include only the new header (i.e. 
416
          class = "headerfile"><cmath>), the symbols
417
        will only be defined in namespace std::
418
        (and macros will be converted to inline-functions).
419
      
420
      
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 
424
          url="http://www.cantrip.org/cheaders.html">
425
          www.cantrip.org.
426
      
427
    
428
 
429
    
430
      </code></pre></td>
      </tr>
      <tr valign="middle">
         <td>431</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>        <filename class="headerfile"><fstream></filename> does</code></pre></td>
      </tr>
      <tr valign="middle">
         <td>432</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>        not define <command>std::cout</command>,</code></pre></td>
      </tr>
      <tr valign="middle">
         <td>433</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>        <command>std::cin</command> etc.
434
      
435
        In earlier versions of the standard,
436
        <fstream.h>,
437
        <ostream.h>
438
        and <istream.h>
439
        used to define
440
        cout, cin and so on. Because
441
        of the templatized iostreams in libstdc++-v3, you need to include
442
        <iostream>
443
        explicitly to define these.
444
      
445
    
446
  
447
 
448
  
449
    Iterators
450
    
451
      The following are not proper uses of iterators, but may be working
452
      fixes for existing uses of iterators.
453
      
454
        you cannot do
455
            ostream::operator<<(iterator) to
456
            print the address of the iterator => use
457
            operator<< &*iterator instead ?
458
          
459
        
460
        you cannot clear an iterator's reference
461
            (iterator = 0) => use
462
            iterator = iterator_type(); ?
463
          
464
        
465
        if (iterator) won't work any
466
            more => use if (iterator != iterator_type())
467
            ?
468
        
469
      
470
    
471
  
472
 
473
  
474
    </code></pre></td>
      </tr>
      <tr valign="middle">
         <td>475</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>      Libc-macros (i.e. <command>isspace</command> from</code></pre></td>
      </tr>
      <tr valign="middle">
         <td>476</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>      <filename class = "headerfile"><cctype></filename>)
477
    
478
      Glibc 2.0.x and 2.1.x define the
479
      <ctype.h>
480
      -functionality as macros (isspace, isalpha etc.). Libstdc++-v3
481
      "shadows" these macros as described in the 
482
        linkend="sec-cheaders" endterm="sec-cheaders.title">section about
483
        c-headers.
484
    
485
    
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:
489
      
490
        #include <cctype>
491
        int main() { std::isspace('X'); }
492
      
493
      will result in something like this (unless using g++-v3):
494
      
495
        std:: (__ctype_b[(int) ( ( 'X' ) )] & (unsigned short int)
496
        _ISspace )  ;
497
      
498
    
499
    
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 ?).
503
    
504
    
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 
508
        class="headerfile"><ctype.h> to define functions
509
      instead of macros:
510
      
511
        // This keeps isalnum, et al from being propagated as macros.
512
        #if __linux__
513
        #define __NO_CTYPE 1
514
        #endif
515
 
516
        [ now include <ctype.h> ]
517
      
518
    
519
    
520
      Another problem arises if you put a using namespace
521
        std; declaration at the top, and include 
522
        = "headerfile"><ctype.h>. This will result in
523
      ambiguities between the definitions in the global namespace
524
      (<ctype.h>) and the
525
      definitions in namespace std::
526
      (<cctype>).
527
    
528
    
529
      The solution to this problem was posted to the libstdc++-v3
530
      mailing-list:
531
      Benjamin Kosnik bkoz@redhat.com writes:
532
      
533
        --enable-cshadow-headers is currently broken. As a result, shadow
534
        headers are not being searched....
535
      
536
      This is now outdated, but gcc 3.0 still does not have fully
537
      compliant "shadow headers".
538
    
539
  
540
 
541
  
542
    State of streams
543
    
544
      At least some older implementations don't have
545
      std::ios_base, so you should use
546
      std::ios::badbit, std::ios::failbit
547
      and std::ios::eofbit and
548
      std::ios::goodbit.
549
    
550
  
551
 
552
  
553
    vector::at is missing (i.e. gcc 2.95.x)
554
    
555
      One solution is to add an autoconf-test for this:
556
      
557
        AC_MSG_CHECKING(for container::at)
558
        AC_TRY_COMPILE(
559
        [
560
        #include <vector>
561
        #include <deque>
562
        #include <string>
563
 
564
        using namespace std;
565
        ],
566
        [
567
        deque<int> test_deque(3);
568
        test_deque.at(2);
569
        vector<int> test_vector(2);
570
        test_vector.at(1);
571
        string test_string("test_string");
572
        test_string.at(3);
573
        ],
574
        [AC_MSG_RESULT(yes)
575
        AC_DEFINE(HAVE_CONTAINER_AT)],
576
        [AC_MSG_RESULT(no)])
577
      
578
      If you are using other (non-GNU) compilers it might be a good idea
579
      to check for string::at separately.
580
    
581
  
582
 
583
  
584
    Using std::char_traits<char>::eof()
585
    
586
      
587
        #ifdef HAVE_CHAR_TRAITS
588
        #define CPP_EOF std::char_traits<char>::eof()
589
        #else
590
        #define CPP_EOF EOF
591
        #endif
592
      
593
    
594
  
595
 
596
  
597
    Using string::clear()/string::erase()
598
    
599
      There are two functions for deleting the contents of a string:
600
      clear and erase (the latter
601
      returns the string).
602
      
603
        void
604
        clear() { _M_mutate(0, this->size(), 0); }
605
      
606
      
607
        basic_string&
608
        erase(size_type __pos = 0, size_type __n = npos)
609
        {
610
        return this->replace(_M_check(__pos), _M_fold(__pos, __n),
611
        _M_data(), _M_data());
612
        }
613
      
614
      The implementation of erase seems to be more
615
      complicated (from libstdc++-v3), but clear is not
616
      implemented in gcc 2.95.x's libstdc++, so you should use
617
      erase (which is probably faster than
618
      operator=(charT*)).
619
    
620
  
621
 
622
  
623
    GNU Extensions ostream::form and istream::scan
624
    
625
      These     are not supported any more - use
626
      
627
        stringstreams instead.
628
    
629
  
630
 
631
  
632
    Using stringstreams
633
    
634
      Libstdc++-v3 provides the new
635
      i/ostringstream-classes, (
636
        class="headerfile"><sstream>), but for compatibility
637
      with older implementations you still have to use
638
      i/ostrstream (
639
        class="headerfile"><strstream>):
640
      
641
        #ifdef HAVE_SSTREAM
642
        #include <sstream>
643
        #else
644
        #include <strstream>
645
        #endif
646
      
647
      
648
         strstream is considered to be
649
            deprecated
650
          
651
        
652
         strstream is limited to
653
            char
654
          
655
        
656
         with ostringstream you don't
657
            have to take care of terminating the string or freeing its
658
            memory
659
          
660
        
661
         istringstream can be re-filled
662
            (clear(); str(input);)
663
          
664
        
665
      
666
    
667
    
668
      You can then use output-stringstreams like this:
669
      
670
        #ifdef HAVE_SSTREAM
671
        std::ostringstream oss;
672
        #else
673
        std::ostrstream oss;
674
        #endif
675
        oss << "Name=" << m_name << ", number=" << m_number << std::endl;
676
        ...
677
        #ifndef HAVE_SSTREAM
678
        oss << std::ends; // terminate the char*-string
679
        #endif
680
        // str() returns char* for ostrstream and a string for ostringstream
681
        // this also causes ostrstream to think that the buffer's memory
682
        // is yours
683
        m_label.set_text(oss.str());
684
        #ifndef HAVE_SSTREAM
685
        // let the ostrstream take care of freeing the memory
686
        oss.freeze(false);
687
        #endif
688
      
689
    
690
    
691
      Input-stringstreams can be used similarly:
692
      
693
        std::string input;
694
        ...
695
        #ifdef HAVE_SSTREAM
696
        std::istringstream iss(input);
697
        #else
698
        std::istrstream iss(input.c_str());
699
        #endif
700
        int i;
701
        iss >> i;
702
      
703
      One (the only?) restriction is that an istrstream cannot be re-filled:
704
      
705
        std::istringstream iss(numerator);
706
        iss >> m_num;
707
        // this is not possible with istrstream
708
        iss.clear();
709
        iss.str(denominator);
710
        iss >> m_den;
711
      
712
      If you don't care about speed, you can put these conversions in
713
      a template-function:
714
      
715
        template <class X>
716
        void fromString(const string& input, X& any)
717
        {
718
        #ifdef HAVE_SSTREAM
719
        std::istringstream iss(input);
720
        #else
721
        std::istrstream iss(input.c_str());
722
        #endif
723
        X temp;
724
        iss >> temp;
725
        if (iss.fail())
726
        throw runtime_error(..)
727
        any = temp;
728
        }
729
      
730
      Another example of using stringstreams is in 
731
        url="../21_strings/howto.html">this howto.
732
    
733
    
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
737
      uses.
738
    
739
  
740
 
741
  
742
    About...
743
    
744
      Please send any experience, additions, corrections or questions to
745
      fnatter@gmx.net or for
746
      discussion to the libstdc++-v3-mailing-list.
747
    
748
  
749
 
750
751
 
752
  
780
 
781

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.