2004  5  27 ո


      Sun[tm] Studio 9ʱļC++ 



          Ŀ¼

       1. ˰汾


       2. 汾֧


       3. 


       4. 


       5. 


       6. ʱ


       7. ʱ




          A. ˰汾

       1. οɿȷÿ汾е C++ 
           
       2. ҵ Solaris[tm] ϵͳװ˲ҵĴ
          ܱ롣ʲô⣿
           
       3. Ϊʲôûҵĳб foo.cc ʱȴյй
          foo.cc ĴϢ;Ϣ
           
       4. Ϊʲôұ -P Ԥѡɵ foo.i ļʱյ
          duplicate definitionĴϢ
           
       5. Ϊڱʱҿƣѿʼʹ C++ еԤ
          ͷҲˣ
           
       6. ΪʲôļʱСļܶࣿ
           
       7. Ϊʲôҽ SPARC V9 浵ӵ̬ʱִϢ Sun
          Studio 8 й
           
       8. Ϣԭǣ"SunWS_cache:Error:Lock attempt failed
          for SunWS_cache"
           
       9.  gcc  KAI ı׼ܲӰ졣ûн
          
           
      10. ΪʲôҴӳյоϢ"ld:warning:symbol
          'clog' has differing types"
           
      11. Ϊʲô -xarch=v8plus  -xarch=v8plusa ҵĶ̳߳
          ʹ STLport
           
      12. ΪʲôΪ abs() ĵþжԣ
           
      13. Ϊʲôʱᱻƻ

    ------------------------------------------------------------------------

       1. οɿȷÿ汾е C++ 

          ÿԱʶԼĺԤ塣ĹӦ
          ЩԤڸ汾֮ȶԣرǼ¼Ϊȶ
          Ĺýӿڡ

          Щһֺð취ǱдһСԲԤ
          겢ʺʹĿĵַҲԱдһα -E
          ĵͬ

          μC++ ûָϡеġꡱȡԤ C++ 
          رǣ__SUNPRO_CC ֵλʮ֡һλ
          Ҫ汾ڶλС汾λ΢汾磬C++ 5.6  0x560

          һЩȤԤ꣺

 
            #ifdef __sun
                Sun compiler
            #endif
            #ifdef
                   Sun C compiler /* __SUNPRO_C value is the version number */
            #endif
            #ifdef __SUNPRO_CC
                   Sun C++ compiler /* __SUNPRO_CC value is the version number */
            #endif
            #ifdef __sparc
            generate code for SPARC architecture
            #endif
            #ifdef __sparcv9
            generate code for SPARC architecture
            #endif
            #ifdef
            generate code for Intel architecture
            #endif

       2. ҵ Solaris ϵͳװ˲ҵĴ벻
          롣ʲô⣿

          ½ Solaris 汾пõµѧܻᵼǰЧ
          Ч

          math.h еĺǰֻ double ͵İ汾µ Solaris
          ͷͿ⻹ float  long double ͵ĳءΪ˱
          ãЩʱʽ㡣磺

            #include <math.h>
            extern int x;
            double      z1 = sin(x); // now ambiguous
            double      z2 = sin( (double)x ); // OK
            float       z3 = sin( (float)x ); // OK
            long double z4 = sin( (long double)x ); //  OK

          еõ Solaris ṩ ANSI C++ 
          math.h ֧֣ Solaris 8  9  libm ʵֵ

              * Solaris 9 sparc (PatchId 111722-04)
              * Solaris 9 i386 (PatchId 111728-03)
              * Solaris 8 sparc (PatchId 111721-04)
              * Solaris 8 i386 (PatchId 112757-01)

       3. Ϊʲôûҵĳб foo.cc ʱȴյй
          foo.cc ĴϢ;Ϣ

          ͷļ foo.h ģȱʡ±ͻ
          C++ ļչfoo.cfoo.ccfoo.Cfoo.cppfoo.c++ļ
          foo ҵԶйϸϢġC++ ûָϡ
          Ϊ ģ嶨Ľڡ

          һַļ foo.ccѡ

              *  .h .cc ļƥ䡣
              *

                ָͨ -template=no%extdef ѡģ嶨ļԶ
                ǣѡöģ嶨C++ ׼
                ʵʩڲҶıĴȷ
                ģ嶨壬Ͳʹöģˡ

                ģ嶨ģ͵ĽһۣμC++ ûָϡ5.2.1 
                5.2.2 ڣڶͶģ͵˵ָ룬μC
                ++ ûָϡ

       4. Ϊʲôұ -P Ԥѡɵ foo.i ļʱյ
          duplicate definitionĴϢ 

          ȱʡ£ģ嶨屻ģ嶨ģ͵Ľ
          һۣμC++ ûָϡ5.2.1  5.2.2 ڣڶ
          Ͷģ͵˵ָ룬μC++ ûָϡ

           .h  .i ļпûģ嶨ʱѰ
          ģ嶨ļ .h  .i ļͬչΪΪ cccCc++
           cpp ļᱻΪģ嶨塣ļԶ
           C++ ֲ

          ٶļ foo.cc -P й룬Ӷ foo.iģ
          ûж塣Ҷļҵ foo.cc
          foo.cc ļԶܵ³ظĶ塣

          ָͨ -template=no%extdef ѡرģ嶨ı
          ܡǣѡöģ嶨C++ ׼ʵ
          ʩڲҶı

          £Ҫ .i ļֻļһ
          ΨһƼɡȻöģ롣磺

            CC -P foo.cc
            mv foo.i foo_prep.i
            CC -c foo_prep.i

          ע⣬ʹ -E ѡԤļ޷ The -E
          ѡ¼ļļ .cc ļԿԱԶ֡

       5. Ϊڱʱҿƣѿʼʹ C++ еԤ
          ͷҲˣ

          ʹԤͷܱ֤ٱٶȡԤͷһЩ
          ֱӱļʱֿǲڵġҪõܣԤͷ
          һЩԤȡ

          磬ܴܿԤеõ洦ĳǰϵͳͷ
          iostreamSTL ͷĿͷĳЩļаĴ
          롣Щͷᱻΰұֻȷ޹ʱ
          ɨļϵͳͷͨٸҪչĺꡣ

          ʹԤͷζŴһļļ޹صĶذ
          ݺ͸ӿհ׿ռ䡣ͷеĺԤչġͨ
          ʡڱʱҪ塣

       6. ΪʲôļʱСļܶࣿ

          ļĴСܲ⣬ӳٵԭ

              * ļкĴСŻļ

                Ż̶ȸߵĴļҪʱ䴦Ҫڴ档
                չʽʹôĺ꣬СĺںչԺ
                ܻúܴ

                κŻı루 -xO  -O ѡܿ
                ɣܾļһ򼸸ܴĺҪʱ
                ڴȥŻ

                ⣬ȷڱļдڴС
                û㹻ڴ棬Ż״̬᲻ȶ

              * 

                C  C++ڱڼ䵱ꡣչΪ
                ʱܻתΪ롣Ȼ󣬱һĺ
                С

                úʱĴٶȻ졣ȻɵĴҲ
                еĸ

                йϸϢμC++ ûָϡе -xinline ˵
                ʹ

              * C++ ģ

                C++ ģ浼±õģΪɴ롣һԴ
                ܻҪһģ庯 ģ屾
                ؽ˱ٶȣǱ̫ĴҪ
                ҪȴԭʼԴ￴Ҫࡣ

                磬˺ı׼⣬ôĴ

                cout << "value = " << x << endl;

                ± 241 

       7. Ϊʲôҽ SPARC V9 浵ӵ̬ʱִϢ Sun
          Studio 8 й

          V9 µȱʡַģ -xcode=abs44ǰ
          -xcode=abs64 ܡǣµĴģڶ̬в
          á

              *  -xcode=pic13  -xcode=32 ±ļ˷
                ϿȡҼʼȷġ

              *  -xcode=abs64 ±ļ˷ᵼ¶̬ⲻ
                ÿ̶ڸƵڴеĶʱԿ
                д˷ڳϸƺϵͳ̶ȵ͵
                еӦó

       8. Ϣԭǣ"SunWS_cache:Error:Lock attempt failed
          for SunWS_cache"

          ģ建ġlock attempt failedϢҪԭ

              * ʱĳַʽжϻֹͷڻб
                ܻԭеı汾зµİ
                Լԭбĵǰȷ۱Ժ
                ʽ˳ܵͷšɾļܻ
                ᵼ⡣ȫ޸ɾģ建档

              * ģļɱд롣йϸϢμ
                umask(1) ֲҳرǣȷ洴
                ļĽ̵ umask ҪͬһĽ̽д
                롣Ŀ¼δװ NFS ļϵͳϣϵͳͱԿɶ
                дܽаװ

       9.  gcc  KAI ı׼ܲӰ졣ûн
          

          ʱָµ C++ ѡ -sync_stdio=no 
          ⣬ sync_with_stdio(false) ӵò±롣

          stdlib 2.1.1 Ҫȱʡ½ C stdio  C++
          ͬÿ cout ˢ¡ĳ cout
          ˴ stdout ûУȵĻˢ¿ܻἫ
          ӰʱܡC++ ׼ҪΪʵʩ
          ׼ĳ˵˴ͬ⣺еǰӡ
          Hello beautiful world

            #include <iostream>
            #include <stdio.h>
            int main()
                {
            std::cout << "Hello ";
            printf("beautiful ");
            std::cout << "world";
            printf("\n");
                }

           cout  stdout л壬Ϳܻҡ

          ±ִļʱָµ C++ ѡ
          -sync_stdio=noѡᵼ sync_with_stdio( ) ڳʼʱ
          ãκ֮ǰ

          ±룬Ҫκγ֮ǰָͨҪͬ
          Ӷ sync_with_stdio(false) ĵáһʾã

            #include <iostream>
            int main(int argc, char** argv) {
                 std::ios::sync_with_stdio(false);
            }

          sync_with_stdio ĵӦеĵһá

          й -sync_stdio ϸϢμC++ ûָϡ C++ ֲҳ
          CC(1)

      10. ΪʲôҴӳյоϢ"ld:warning:symbol
          'clog' has differing types"

          ͬһ libm.so.2 ; iostream ʱһ
          ŵӳ򾯸棬вͬࡣԺԴ˾Ϣ

           Solaris 10 еȱʡѧ libm.so.2 C99 ׼
          Ҫȫƿռеĸ־clogָͨ
          -compat=4  -library=iostream ʹ C++  iostreams
          ȫƿռеõ׼clog׼ iostreams û
          д˳ͻš

          ҪͷͿԱԾĬʽΪÿclog
          ͬһʹȻǱÿнԭʼ
          ƴдΪ΢ţԲԭʼݵĶļԼӡ

          Ҫȷõ iostream ѧʵϵͳͷԼ
          Щʵ塣

      11. Ϊʲô -xarch=v8plus  -xarch=v8plusa ҵĶ̳߳
          ʹ STLport

          ʹ -xarch=v8plus  -xarch=v8plusa ʱSTLport 
          ֹȷĶ̲߳ѱ޸޸ STLport
          ͷеĸģԼһЩ STLport ĸġʹ STLport ͷ
          ڰ汾бĴ룬ӵ³ǰҪ C++ 5.6 
          汾Ĳ±롣

      12. ΪʲôΪ abs() ĵþжԣ

          26.5 е C++ ׼Ҫ abs أ

              * In <stdlib.h> and <cstdlib>

                int  abs(int);
                long abs(long);

              * <math.h> and <cmath>

                float       abs(float);
                double      abs(double);
                long double abs(long double);

          ֱabs  Solaris ϿõΨһ汾һֱǴͳ int
          汾 absκֵͣתΪ
          int abs  int 汾ãٶ <stdlib.h> 
          <cstdlib>

          Solaris ͷͿ¸ڷйѧ C++ ׼

          磬 <math.h>  <stdlib.h>, ҵô
           abs, ͻѡ汾һֵת
          ΪһͣתȼһġοC++ ׼
           13.3.3˲ȷʹ÷ C++ ׼ı
          һĴϢ

          ô abs Ӧ׼ͷ <stdlib.h> 
          <cstdlib> ȷõйȷʹøֵ abs
          ͻӦ <math.h>  <cmath>

          һ򵥵Ƽϰ <math.h>  <cmath>
          Ҫ <stdlib.h>  <cstdlib>

          ƵĿѧ cos  sqrtSolaris ͷͿ
          ѭ C++ ׼ṩ floatdouble  long double ذ
          磬ôֵ sqrtΪֻһ sqrt 汾
          ãԭȱĴ롣汾ãͱ뽫
          ֵĸ͡

            double root_2 = sqrt(2); // error
            double root_2 = sqrt(2.0); // OK
            double x = sqrt(int_value); // error
            double x = sqrt(double(int_value)); // OK

      13. Ϊʲôʱᱻƻ

          ʱΪ˷ʱʱΪԹҪ
          磬صֵһʱתĽһʱ
          

          ԭʼ C++ ʱ ("temp")ʱ٣ֱ
          ʱSun C++ ڿĽβرţʱļ

          ۼC++ ίԱһ̶ĵطʱļ
          ʱļʽĽβָͨʽĽβ
           C++ ׼еĹ

          Ƿ֣ʹ Sun C++ ģҲǹģʱļ
          ʱӦأñȱʡΪȱʡ
          µʱڴǵĿĽβ١

          Ҫϱ׼ΪʱļڴȫʽĽβ
          ٣ʹñʱѡ -features=tmplife

          һֱʹøѡģдʱ
          ڱʱЧѡڸģеıʽββ١



          B. 汾֧

       1. ׼ I/O ʹͳ I/O кܾʹƼһЩ̲
           
       2. ֪Щ C++ 汾Ǽݵģ
           
       3. Щ RogueWave ѱ׼ Sun Studio 9
           
       4. ô֪ЩԼǰĲЩ⣿
           
       5. ǷҪ libC.so.5  libCrun.so.1 Ĳ
           

    ------------------------------------------------------------------------

       1. ׼ I/O ʹͳ I/O кܾʹƼһЩ̲

          ƺʵȫͬġ I/O ı̽Ǻ
          ġڸӵĲдԼͺܲһˡ

          汾еĴͳ iostream  C++ 3.x Լ 4.x еİ汾Ǽ
          ġ˱а汾ṩĵ⣬һο飺

              * Steve Teale
				C++ IOStreams Handbook
				Addison-Wesley 1993

          ׼ iostream  C++ ׼ġ˱汾ṩ
          ⣬ܺõĲο飺

              * Nicolai Josuttis
                The C++ Standard Library
                Addison-Wesley 1999
                ȫ C++ ׼Ľ̲ģ


              * Angelika Langer  Klaus Kreft
                Standard C++ IOStreams and Locales
                Addison-Wesley 1999
                ֻ iostream ԻĽ̡̳

          汾 iostream ʵּ I/O Դ뿴һġΪ
          ʹתףǶԱ׼ iostream ṩ˷Ǳ׼ͷļ
          <iostream.h><fstream.h>  <strstream.h>ṩһ׺ʹ
          ͳ iostream ƵȫƿռʹЩͷȱʡ
          ¾ͻõ׼ iostreamҪþ iostreamӦ
          -library=iostream бӡ

          磬Ȼеıʹǵı
          ´ڴͳ iostream Ҳڱ׼ iostream

           #include <iostream.h>

           class myclass {
           public:
           myclass(int i) :k(i) { }
           friend ostream& operator<<(ostream&, const myclass&);
           private:
           int k;
           };

           // user-written output operator
           ostream& operator<<(ostream& os, const myclass& m)
           {
           os << m.k;
           return os;
           }

           int main() {
               // simple I/O using cout, cin
               cout << "Enter a number:" << endl;
               int val;
               if( !(cin >> val) ) {
                   cout << "Invalid entry, using zero" << endl;
                   val = 0;
               }

               // using the user-written output operator
               myclass m(val);
               cout << "Value is " << m << endl;
           }

          ôзеκһ֣ʹ Sun бУ

example% CC -library=iostream example.cc # standard mode with classic iostreams
example% CC -library=iostream example.cc # standard mode with classic iostreams
example% CC -compat=4 example.cc # C++ 4.2 compatbility mode

       2. ô֪Щ C++ 汾Ǽݵģ

          ȣһ£ϼݡָýϱĶ
          ý±ĴӣֻҪʱõı
          µı

          C++ 4.04.1  4.2 ϼݵġC++ 4.2 ֲһЩ
          汾֮ġַ顱⡣

          ģʽ (-compat) µ C++ 5.05.65.25.35.4  5.5 
           4.2 ϼݵġC++ 4.2  5.0  5.6 ʵ
          ȫݵģ±͵ĵϢ (stabs) 
          ĵǲݵġ

          ȱʡı׼ģʽµ C++ 5.0  5.6 ϼݵġʵʶ
          ȫݵģ±͵ĵϢ (stabs) 
          ĵǲݵġ

       3. Щ RogueWave ѱ׼ Sun Studio 9

          ޷ɿظЩӦ׼ƷЩ汾ǵı
          һЩ汾ȷѡ
          ͹ӦϵǷ C++ κض汾
          Ʒ

          ĳЩ RogueWave ǵı͵ģ˵һ
          ͵İ汾ȷϵġ

       4. ô֪ЩԼǰĲЩ⣿

          ڲƷϢʿԱڣַΪ
          http://developers.sun.com/prodtech/cc

          ƷԴ http://sunsolve.sun.com ء

       5. ǷҪ libC.so.5  libCrun.so.1 Ĳ

          һ˵Solaris[tm] ϵͳЩ°汾һ͡
          ǣ޸ĴһЩЩ⾭вЩ
          ۻԵĲݣǻµĲ
          ַ http://sunsolve.sun.com± 2004  7 
		  Ĳ ID 

          ݿµΪ SUNWlibC32 λ
          SUNWlibCx64 λ libCrun ΪPatch
          DescriptionsǡAll Sun Internal Collections
          бȱʡá

           1libC  libCrun 
		   ID       Solaris ϵͳ      ܹ

          108434-17         8                     SPARC/v8

          108435-17         8                     SPARC/v9

          108436-15         8                     x86

          111711-11         9                     SPARC/v8

          111712-11         9                     SPARC/v9

          111713-08         9                     x86



          C. 

       1. ܻʹüģʽ (-compat) Ĵ׼ģʽĴ
           
       2. ôܽ C++  C ĳ F77F90  F95 ĳϣ
           

    ------------------------------------------------------------------------

       1. ܻʹüģʽ (-compat) Ĵ׼ģʽĴ

          ǽ鲻Ҫͬһлϴ룬Ҳ֧ʹ
          ͨ򡱻̬װ⡣ԭ£

              * Ĳֲͬ

              * ĵ˳ͬ

              * ַ顱ͬ

              * 쳣ķгͻ

              *  iostream ӵͬһļ⡣

          ʹ֣ģʽͱ׼ģʽͨţ
          εط׳쳣Ҳܻ

          ĳЩ԰Ѽģʽͱ׼ģʽĿļӵһ
          ڱ渽ġC++ Ǩָϡϸ˵뿴һе
          Ͼɶļ¶ļָϿɴ
          http://docs.sun.com á

       2. ôܽ C++  C ĳ F77F90  F95 ĳϣ

           Workshop 6 update 1汾 5.2ʼʹ
          -xlang={f90|f95|f77} ѡѡҳо
          ҪЩԼЩҪԺ˳֡

          -xlang ѡ C вáҪ C  Fortran ̣
           cc 벢 Fortran ӡ



          D. 

       1. ΪʲôԱ׼쳣౨溬岻壿
           
       2. Ϊʲô C++ 5.3 ҵ⺯׳ָʹ
           
       3. Ϊʲôӳʱᶪʧģʵʵƺģ建С
           
       4. Ϊʲôʹ +w2 ʱֺδչľ棬ʹ +w2 +d 
          ᣿
           
       5. ʹ -ptr ѡöģϵͳϢ⣬ڲͬĿй
          ϵͳϢܣҸô
           
       6. Ϊʲô fprintf("%s",NULL) ᵼ¶ιϣ
           
       7. ҵ sqrt() Ĳͬʽõĸƽ鲿ķҲ
          ͬʲôԭ
           
       8. ģеһԪδʵҳʱ
          C++ 5.0 ǿԵģΪʲôڲˣ
           
       9. Ϊʲô˵һеһԱ޷Ƕ׵зʣ
           
      10. ʱ֡⺯áϢʲôԭ
           
      11. Ϊʲôʾ⺯òͬǩ˻⺯
          ҵûжԴ˴뱨
           

    ------------------------------------------------------------------------

       1. ΪʲôԱ׼쳣౨溬岻壿

           Solaris У׼ͷļ <math.h> Խṹ쳣һ
          Ǳ׼ Unix Ҫġͨʹʹָ C++ ı
          ׼쳣ȫַΧУɳͻ

//  1

#include <math.h>
#include <exception>
using namespace std; // using-declaration
exception E;  // error, exception is ambiguous

//  2

#include <math.h>
#include <exception>
using std::exception; // using-directive
exception E;  // error, multiple declaration for exception

          ʹתʹָвͬԴϢȫһ

          취

             1.  <cmath>  <math.h> Solaris У<cmath>
                ֻ C  C++ ׼ָҪ <math.h> 
                Unix ԣͲʹ취


             2. д using std::exception;ʹ <math.h> 
                ʽд std::exception ʹ typedef ʱ׼
                쳣࣬

#include <math.h>
#include <exception>
std::exception E; // OK
typedef std::exception stdException; // OK
stdException F; // OK

             3. Ҫд using namespace std;
                C++ ƿռ׼̫࣬ʵʴʹָ
                ʱܻܿӦóĿͻC++ 
                ̵鼮ʱʹָСʾĴСʹ
                õʹʽ޶ơ


       2. Ϊʲô C++ 5.3 ҵ⺯׳ָʹ

          5.3 C++ ¼һ C++ е⺯ֻ
          ǵĺ쳣ǺԿԸߣ
          ͡ʾ

class Base {
public:
	// might throw an int exception, but no others
	virtual void f() throw(int);
};
class Der1 :public Base {
public:
virtual void f() throw(int); // ok, same specification
};
class Der2 :public Base {
public:
virtual void f() throw(); // ok, more restrictive
};
class Der3 :public Base {
public:
virtual void f() throw(int, long); // error, can't allow long
};
class Der4 :public Base {
public:
virtual void f() throw(char*); // error, can't allow char*
};
class Der5 :public Base {
public:
virtual void f(); // error, allows any exception
};

          δ C++ ԭ

#include "base.h" // declares class Base
void foo(Base* bp) throw()
{
try {
bp->f();
    }
catch(int) {
    }
}

           Base::f() Ϊֻ쳣foo Բ
          κ쳣롣˺ Der5 ࣬
          еĸǺκ쳣һ Der5 ָ봫ݸ foo
          ʹ foo ʱɼûκ⣬foo ҲЧ

       3. Ϊʲôӳʱᶪʧģʵʵƺģ建С
           

          ģ建汣˱ĿļбԼе
          ģʵ Note, however, that the compiler now only uses the
          template cache when you specify -instances=extern.ƶ
          ĿļĿļϲУͻϿ뻺ӡ
          취

              * ֱĿ¼Ŀļģ建潫ͬһĿ¼¡

                Ҫ

example% CC -c -instances=extern f1.cc
example% mv f1.o /new/location/for/files

                Ҫ

example% CC -c -instances=extern f1.cc -o /new/location/for/files/f1.o

                Խ̷װ makefile С

              *  CC -xar м鵵 (.a) ļÿ鵵ͻ
                鵵жʹõģʵȻٰЩ鵵
                ճСЩģʵڲͬĹ鵵ظˣ
                ֻΪÿ鵵һʵ

example% CC -c -instances=extern f1.cc f2.cc f3.cc
example% CC -xar f1.o f2.o f3.o -o temp1.a
example% CC -c -instances=extern f4.cc f5.cc f6.cc
example% CC -xar f4.o f5.0 f6.0 -o temp2.a
example% CC -c -instances=extern main.cc
example% CC main.o temp1.a temp2.a -o main

       4. Ϊʲôʹ +w2 ʱֺδչľ棬ʹ +w2 +d 
          ᣿

          C++ ɷɵ C++ Լ
          뷢ɵŻC  Fortran ֻŻ
          ƽ̨ϵбʹͬĴ뷢

          C++ ķ᳢չκʽΪĺ
           ̫󣬷ֻʹ +w2 ѡʱŻᷢ;档+d
          ѡֹκκʹ +dͲ־
          档-g ѡҲر C++ -xO ѡӰ
          

          ŻڱԡѡŻ (-xO4) ߼
          𣬴뷢ͻеĺԴ
          ֻҪ뷢ΪͻѺ滻롣
          κιŻʧܣϢ+d ѡӰ
          

       5. ʹ -ptr ѡöģϵͳϢ⣬ڲͬĿй
          ϵͳϢܣҸô

          5.0  5.6 汾֧ -ptr ѡ 4.2 汾ѡ
          Ľûϣһ˺ܶ⡣

          òҪڲͬĿйϵͳϢ⡣ϵͳϢ
          ܱܿͼءһĿ¼ֻܱһ
          Ŀ벻ͬĿصĶļӦʹòͬĿ¼

           5.0 濪ʼĿļĿ¼зһģϵͳ
          Ϣ⡣һĿʹöϵͳϢ⣬Ӧϵͳ
          ϢĿ¼һĿļʱĿļص
          ϵͳϢԶģʵҪκαѡ

       6. Ϊʲô printf("%s",NULL) ᵼ¶ιϣ

          ЩӦóΪַָӦַָָ
          ЩӦóзʿַָʱͻᷢΥ档

          мԭ²ʹ *printf() ϵָ롣а
          ڣԭ

              * ɰȫļʹԱΪѿָ봫ݵ
                printf() ûġ

              * Աд޷ֲĴ롣ANSI CXPG3XPG4
                SVID2  SVID3 涨 printf("%s", pointer) Ҫʹָָ
                յַֹ顣

              * ʹõԸѡԱѿָ봫ݵ printf() 
                ҳ򶪵ģõԹҵĸ printf() 
                ˴ָ롣ǣ printf() ͨӡ(null
                pointer)˴еܻܿŷ
                롰(null pointer)ʵڵȴʵݡʱ
                ܾ޷ȷʵ⵽ںδˡ

          ĳӦóѿָ봫ݸ *printfʹһر
          Ŀ /usr/lib/0@0.so.1ṩλ 0  0 ֵĻơ
          ⽫ǣ浽κ͵ĿָǹĴˣ
          ֻӦΪ֮ǰʱ취

       7. ҵ sqrt() Ĳͬʽõĸƽ鲿ķҲ
          ͬʲôԭ

          ʵǰ C99 csqrt  G 淶еġ磬
          д뷶

          complex sqrt (3.87267e-17, 0.632456)
          float sqrt (3.87267e-17, -0.632456)

              * ڼģʽʹ libcomplex ʾ

#include <iostream.h>
#include <math.h>
#include <complex.h>
 
int main()
{
complex ctemp(-0.4,0.0);
complex c1(1.0,0.0);
double  dtemp(-0.4);
cout<< "complex sqrt "<< sqrt(ctemp)<<endl;
cout<< "float sqrt   "<< sqrt(c1*dtemp)<<endl;
}

              * ڱ׼ģʽʹ libCstd ʾ

#include <iostream>
#include <math.h>
#include <complex>
 
using namespace std;
 
int main()
{
complex<double> ctemp(-0.4,0.0);
complex<double> c1(1.0,0.0);
double  dtemp(-0.4);
cout<< "complex sqrt "<< sqrt(ctemp)<<endl;
cout<< "float sqrt   "<< sqrt(c1*dtemp)<<endl;
}

              *  sqrt  atan2 ʵֵġʾʹ
                atan2 ⡣Ϊ

c=-0.000000  b=-0.400000  atan2(c, b)=-3.141593
a=0.000000  b=-0.400000  atan2(a, b)=3.141593

                һ£atan2 ΪһΪȡ
                ڵһݵĲ -0.0  0.0

#include <stdio.h>
#include <math.h>
 
int main()
{
double a = 0.0;
double b = -0.4;
double c = a*b;
double d = atan2(c, b);
double e = atan2(a, b);
printf("c=%f  b=%f  atan2(c, b)=%f\n", c, b, d);
printf("a=%f  b=%f  atan2(a, b)=%f\n", a, b, e);
}

       8. ģеһԪδʵҳʱ
          C++ 5.0 ǿԵģΪʲôڲˣ

          鷶 C++ 5.0 ûдģڽ
          °汾ıȴᵼʱ

example% cat t.c

#include <ostream>

using std::ostream;

template <class T>
class TList {
public:
friend ostream& operator<< (ostream&, const TList&);
};

template <class T>
ostream& operator<< (ostream& os, const TList<T>& l)
{
return os;
}

class OrderedEntityList {
public:
TList<int> *Items;
ostream& Print(ostream &) const;
};

ostream&
OrderedEntityList::Print(ostream& os) const
{
os << *Items;
return os;
}

main()
{
}

example% CC t.c

Undefined			first referenced
symbol			    in file
std::basic_ostream<char,std::char_traits<char>
>&operator<<(std::basic_ostream<char,std::char_traits<char> >&,const
TList<int>&) 4421826.o

ld:fatal:Symbol referencing errors.No output written to a.out

          ձ׼鷶Чġڣ漰κģ
          ʵ

friend ostream& operator<< (ostream&, const TList&);

          漰κģʵ

          ޶Ʋ޷ƥģʹԪʱǿɼġ
          ҪʹԪƥģ壬ҪΪģ庯޶ơ

          ַԪʱģǿɼġ

          ܵ˵Ԫ漰ģ壬һ뺯ƥ
          涼ͬ·ģ庯ģ庯á

          ĴЧģ

template <class T> class TList;
// so we can declare the operator<< template

template <class T>
ostream&
operator<< (ostream& os, const TList<T>& l)
{ 
return os;
} 

template <class T>
class TList {
public:
// note the scope qualification on the function name
friend ostream& ::operator<< (ostream&, const TList&);
};

       9. Ϊʲôʾ޷Ƕ׵зʷеĳԱ

class Outer {
typedef int my_int;
static int k;
class Inner {
my_int j;          // error, my_int not accessible
int foo() {
return k; // error, k not accessible
		}
	};
};

           ARM  C++ ׼ǶԷװеĳԱûرķȨ
           my_int  k  Outer ˽еģֻ Outer Ԫ
          ܷǡҪʹǶΪԪǰ࣬Ȼʹ
          ΪԪʾ

class Outer {
typedef int my_int;
static int k;
															
	// add these two lines ahead of the class definition
class Inner;
friend class Inner;
																			
class Inner {
my_int j;         // OK
int foo() {
return k; // OK
		}
	};
};

      10. ʱ֡⺯áϢʲôԭ

          ֡⺯áϢ϶Ϊд󡣴
          ʽеһַ

              * ӳĹ캯ѡthisݵⲿ
                ᵼִڹʱthisǹ캯
                ͣչ͡Ȼܻ
                ͼô⺯ʾ

class Abstract;

void f(Abstract*);

class Abstract {
public:
virtual void m() = 0; // pure virtual function
Abstract() { f(this); }   // constructor passes "this"
};

void f(Abstract* p)
{
p->m();
}

                캯 f ʱthisΪAbstract*Һ
                 f ͼô⺯ m

              * ⣬Եδʽ޶Ĵ⺯Ҳᵼ
                󡣿Ը⺯ṩ壬ֻͨڵʱ޶
                Ƶķʽãƹûơ

class Abstract {
public:
virtual void m() = 0; // body provided later
void g();
};

void Abstract::m() { ...} // m Ķ

void Abstract::g()
{
m(); // error, tries to call pure virtual m
Abstract::m(); // OK, call is fully qualified
}

      11. Ϊʲôʾ⺯òͬǩ˻⺯
          ҵûжԴ˴뱨

          C++ ĹֻһΧڷ緶Χ౻
          ΪΧķΧڡκƶ
          ضػеκκC++ Ļ ARM

          ûбʵϻΣΪ˴벢
          Ԥڵǵıյ˴ʱᷢ档˴
          ǺϷģܲҪĽ

          ؼаຯͱѻຯǰΧ
          Сȱʡı׼ģʽ½б룬Լһʹ

class Base {
public:
virtual int    foo(int);
virtual double foo(double);
};

class Derived :public Base {
public:
using Base::foo; // add base-class functions to overload set
virtual double foo(double); // override base-class version
};



          E. 

       1. ܵõһȫϱ׼ C++ ׼ (stdlib)ǰ
          libCstd ֧Щܣ
           
       2. Ҫ C++ ׼ģ (STL)Ҵܵõûмģʽ
          (-compat) ı׼ģ⣿
           
       3. libCstd ȱЩ׼⹦ܣ
           
       4. ȱٱ׼⹦ܻʲô
           
       5. ûڱ׼ tools7 汾ڻ tools8 
           

    ------------------------------------------------------------------------

       1. ܵõһȫϱ׼ C++ ׼ (stdlib)ǰ
          libCstd ֧Щܣ

          а汾 STLport ׼ʵְ汾 4.5.3Ϊѡ׼
          ⡣STLport õܣ C++ ׼һһЩ
          ܻӭչǣڶȱʡʹõı׼Ⲣݡ

          ǰ libCstd Ϊ C++  5.0 ġ汾֧
          ģΪĳԱ׼ĳЩҪԱģ壬ζȱ
          һЩܡȱٵĹܴ󲿷ֳھй캯ģ
          תСԴбдʽתΪ취

           5.1 濪ʼC++ ֧ģΪĳԱҿַ֧
          ϱ׼Ŀ⡣޷ƻԴͶƼԶ¿⣬
          ʹͬƵ libCstd

           gnu  SGI վҵ׼Ĺʵ֣ҲԴ
          RogueWave  Dinkumware ȹӦﹺ⡣ο STL
          ⡣

       2. Ҫ C++ ׼ģ (STL)Ҵܵõûмģʽ
          (-compat) ı׼ģ⣿

          C++ ֧ STLport ׼ʵְ汾 4.5.3libCstd 
          ȱʡ⣬ STLport Ʒɹѡ񡣱а汾Ȱһ
          Ϊ libstlport.a ľ̬鵵ҲһΪ libstlport.so Ķ
          ̬⡣

          ıѡر libCstd ʹ STLport

          -library=stlport4

          ȱʡ C++ ׼ libCstd Լ STLport  STLʹ
          ͬ汾ı׼⣬ôзյĶҲܱ֤õĽ

          Ҫ벻ͬ STLӦʹ -library=no%Cstd ѡָ
          ͷļѡĿ⡣ûб iostreams ⣬
          áͳiostreams ׼ iostreamsӦм
          -library=iostreamϸָʾμ渽ġC++ ûָϡ
          ġ C++ ׼⡱ http://docs.sun.com øָϡ

       3. libCstd ȱЩ׼⹦ܣ

          ԭȣ C++ 5.0 У׼Ĺ첻֧ЩҪог
          ԱģͲרŻԡȻ C++ 5.1 ԺЩԣ
          ڱ׼дǣΪӰԡг˸
          ֹȱٵĹܡ

              * ֹԣԱģ庯

                    o  <complex>  complex У

                      template <class X> complex<T>& operator= (const
                      complex<X>& rhs)
                      template <class X> complex<T>& operator+= (const
                      complex<X>& rhs)
                      template <class X> complex<T>& operator-= (const
                      complex<X>& rhs)
                      template <class X> complex<T>& operator*= (const
                      complex<X>& rhs)
                      template <class X> complex<T>& operator/= (const
                      complex<X>&)

                    o  <utility>  pair У

                      template<class U, class V> pair(const pair<U, V> &p);

                    o  <locale>  locale У

                      template <class Facet> locale combine(const
                      locale& other);

                    o  <memory>  auto_Ptr У

                      auto_ptr(auto_ptr<Y>&);
                      auto_ptr<Y>& operator =(auto_ptr<Y>&);
                      template <class Y> operator auto_ptr_ref<Y>();
                      template <class Y> operator auto_ptr<Y>();

                    o  <list>  list У

                      Աģ

                    o ڴģУ

                      ģ幹캯

              * ֹԣԱģ

                 <memory>  auto_ptr У

                template <class Y> class auto_ptr_ref{};
                auto_ptr(auto_ptr(ref<X>&);

              * ֹԣزרŻĺģ

                 <deque><map><set><string><vector> 
                <iterator> в֧ģ庯ǳԱ

                    o  mapmultimapsetmultisetbasic_string
                      vectorreverse_iterator  istream_iterator ࣺ

                      bool operator!= ()

                    o  mapmultimapsetmultisetbasic_string
                      vector  reverse_iterator ࣺ

                      bool operator> ()
                      bool operator>= ()
                      bool operator<= ()

                    o  mapmultimapsetmultisetbasic_string 
                      vector ࣺ

                      void swap()

              * ֹԣȱʡģĲרŻ

           <algorithm> в֧ģ庯ǳԱ

          count(), count_if()

           <iterator> в֧ģ壺

          template <class Iterator> struct iterator_traits {}
          template <class T> struct iterator_traits<T*> {}
          template <class T> struct iterator_traits<const T*>{}
          template typename iterator_traits::difference_type
          distance(InputIterator first, InputIterator last);

       4. ȱٱ׼⹦ܻʲô

          Щ밴 C++ ׼Чģܱ롣

          ʾǴӳʱеĵһԪؿ const û
          ҪʱԱģ彫ذ pair<T, U> ת
          pair<const T, U>ȱٹ캯ֱ

          ڲӳеĵһԱ򵥵Ľ취
          ʱʹʽ const磬 pair<int, T> 
          <const int, T> map<int, T>  map<const int, T>

       5. ûڱ׼ tools7 汾ڻ tools8 

          ȻУֻ C++ 5.35.45.5  5.6ʹ
          -library=rwtools7_std бӸÿ⡣

          RogueWave Ѹı Tools.h++ ĹʽֻΪ
          SourcePro Ʒһṩûа汾 8  Tools.h++



          F. ʱ

       1. ҷ 5.0  5.1 ıʱǳ ( 4.2 Ƚ)
          ڽڽ
           
       2. ע⵽ 4.2 ȣļҪܶࡣн
          
           
       3. ܷ񽫵һ̷ֲһ˵ദ (MP)
          ϵͳıʱǷǸһЩ
           

    ------------------------------------------------------------------------

       1. ҷ 5.0  5.1 ıʱǳ ( 4.2 Ƚ)
          ڽڽ

           5.1 Ĳ 015.25.35.45.5  5.6 Ѿ
          ˱ʱ䡣Աܲ⣬뿼Ľ飺

             1. һЩ£ᵼ¾޴˷ѡʹ -xO4 
                -xO5 ѡʱ뷢ԶһЩҪʹ
                ýϵ͵Ż -xO3 -xinline ѡ
                ֹŻԶĳЩضĺ

             2. رսϴʽйʽۣμ
                ݡ

       2. ע⵽ 4.2 ȣļҪܶࡣн
          

          -g ĶļԭǴ 5.0 濪ʼɺ
          ģϢ5.1 ıԺֳܶСɵĵ
          Ϣ5.25.35.45.5  5.6 ı˽һĸĽ
          ںܶ£ļĴСС 25%  50% ϡĽҪ
          ʹƿռ䡢ģʹ༶̳еֲṹĴϡ

       3. ܷ񽫵һ̷ֲһ˵ദ (MP)
          ϵͳıʱǷǸһЩ

          Ƕ̵߳ġദϵͳܻһЩΪ
          κһʱ϶кܶС

          ʹ dmake (渽Ĺ֮һ)Ϳͬʱж롣



          G. ʱ

       1. C++ ǷùؼʡinlineǵĺΪʲôұд
          ĺȴǱ
           

    ------------------------------------------------------------------------

       1. C++ ǷùؼʡinlineǵĺΪʲôұд
          ĺȴǱ

          Ӹ˵ inline Ϊָͼú
          5.1  5.6 ı޶㷨ʹĹ졣
          ǣȻ޷ɹư

              *  5.2  5.6  C++ ʼЩִеĺ
                òٱչ仯Ըõƽٶȡ
                Сʱٶȡ

                磬ھ̬ʼõıʽִֻһΣЩ
                ʽĺòٱչע func ھ̬
                ĳʼʽбʱܲչط
                ɱƵأ쳣еĺÿܲչ
                ΪЩ뼫ִС

              * ݹ麯ֻһü𡣱޷ȷ
                麯áǰʵְ취ͣڶκκĵ
                һεϡ  

              * ʱСĵҲԭܵչС
                ̫磬func1  func2 func2 ֵ func3
                ơʹЩÿһСҲûеݹ
                ãϲչСҲܻ̫ʹñ޷չ
                Щ

                ܶ׼ģ庯ǺСģ£
                ֻмûᱻչ

              * Щ goto 䡢ѭ try/catch 
                C++ ǣЩŻ -xO4 
                  

              * C++ ŻĺС
                ơһƼʹơϵ
                ֧˽߻򽵵ʹ˴СƵڲѡ  

              * ⺯ܱʹдδض塣ԭǱ
                ޷֪ı뵥ԪǷͶԸ⺯ض
                塣  

          עĳЩȰ汾Уи if ͷĺܱ
          ѾȡңСȱʡҲ
          ˡĳЩУЩ仯ᵼ¸ĺӶ±
          ɵĴ

          Ҫȫ C++ Ӧʹ +d ѡ

          ŻԸݿȵĽڽϸߵŻ (-xO4) µ
          ԶģǷѺΪ

2004  5  27 ո

------------------------------------------------------------------------

Ȩ (c) 2002 Sun Microsystems, Inc. Ȩ
밴ʹá

