home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!sun-barr!ames!decwrl!usenet.coe.montana.edu!rpi!clarkson!grape.ecs.clarkson.edu!anstey
- From: anstey@sun.soe.clarkson.edu (Charles Anstey)
- Subject: GCC2.2 doesn't inline template member functions FIX (ugly)
- Message-ID: <ANSTEY.92Aug27013835@sunspot.clarkson.edu>
- Sender: news@news.clarkson.edu
- Nntp-Posting-Host: sunspot.ece.clarkson.edu
- Organization: Clarkson University, Potsdam NY
- Distribution: comp.lang.c++
- Date: Thu, 27 Aug 1992 06:38:35 GMT
- Lines: 65
-
- I previously posted that GCC2.2 does not inline template member functions.
- Thanks to a posted reply from someone (sorry, forgot to save who it was), it
- was suggested that GCC would inline the function in all functions following
- the first one so...
- int foo (){ } // Not inlined
- int bar (){ } // inlined
- int bar2 () { } // inlined
-
- After testing the theory, I found it to be true which led me to a fix which
- is rather ugly but the results are correct.
-
- Original example
- template <class PT>
- class S {
- private:
- PT length;
- public:
- S (int size): length(size) {};
- ~S () {};
- PT size () { return (length);};
- };
-
-
- Modified version to make GCC do what it should have done in the first place.
- #pragma interface // Needed to prevent duplicate information
-
- template <class PT>
- class S {
- private:
- PT length;
- public:
- inline S (int size): length(size) {};
- inline ~S () {};
- inline PT size () { return (length);};
- };
- // 'inline' keyword required or GCC will make an external reference
-
- // Now create a dummy inline function which calls all the class' functions.
- // Only works for S<int>. If you also use S<double> you need another
- // inline function.
- inline PT s_dummy (void) { S<int> s(2); return (s.size()); }
-
- Main program
- #include <iostream.h>
- #include "S.h"
-
- int main ()
- {
- S<int> s(4);
- int x;
-
- x = s.size ();
- cout << x << "\n";
- }
-
- Now GCC will produce the desired result with no extra output overhead.
- The trick is to create the inline dummy function which will call every
- member function. However you must make an inline dummy function for
- each type used. I really hope this gets fixed in the next release.
-
- Despite this, I still believe GCC is the best compiler for the money.
-
- -Charles Anstey
-
-
-