home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / cplus / 12955 < prev    next >
Encoding:
Text File  |  1992-08-26  |  2.2 KB  |  78 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!sun-barr!ames!decwrl!usenet.coe.montana.edu!rpi!clarkson!grape.ecs.clarkson.edu!anstey
  3. From: anstey@sun.soe.clarkson.edu (Charles Anstey)
  4. Subject: GCC2.2 doesn't inline template member functions FIX (ugly)
  5. Message-ID: <ANSTEY.92Aug27013835@sunspot.clarkson.edu>
  6. Sender: news@news.clarkson.edu
  7. Nntp-Posting-Host: sunspot.ece.clarkson.edu
  8. Organization: Clarkson University, Potsdam NY
  9. Distribution: comp.lang.c++
  10. Date: Thu, 27 Aug 1992 06:38:35 GMT
  11. Lines: 65
  12.  
  13. I previously posted that GCC2.2 does not inline template member functions.
  14. Thanks to a posted reply from someone (sorry, forgot to save who it was), it
  15. was suggested that GCC would inline the function in all functions following
  16. the first one so...
  17. int foo (){ } // Not inlined
  18. int bar (){ } // inlined
  19. int bar2 () { } // inlined
  20.  
  21. After testing the theory, I found it to be true which led me to a fix which
  22. is rather ugly but the results are correct.
  23.  
  24. Original example
  25. template <class PT>
  26. class S {
  27. private:
  28.   PT length;
  29. public:
  30.   S (int size): length(size) {};
  31.   ~S () {};
  32.   PT size () { return (length);};
  33. };
  34.  
  35.  
  36. Modified version to make GCC do what it should have done in the first place.
  37. #pragma interface  // Needed to prevent duplicate information
  38.  
  39. template <class PT>
  40. class S {
  41. private:
  42.   PT length;
  43. public:
  44.   inline S (int size): length(size) {};
  45.   inline ~S () {};
  46.   inline PT size () { return (length);};
  47. };
  48. // 'inline' keyword required or GCC will make an external reference
  49.  
  50. // Now create a dummy inline function which calls all the class' functions.
  51. // Only works for S<int>.  If you also use S<double> you need another
  52. // inline function.
  53. inline PT s_dummy (void) { S<int> s(2); return (s.size()); }
  54.  
  55. Main program
  56. #include <iostream.h>
  57. #include "S.h"
  58.  
  59. int main ()
  60. {
  61.   S<int> s(4);
  62.   int x;
  63.  
  64.   x = s.size ();
  65.   cout << x << "\n";
  66. }
  67.  
  68. Now GCC will produce the desired result with no extra output overhead.
  69. The trick is to create the inline dummy function which will call every 
  70. member function.  However you must make an inline dummy function for
  71. each type used.  I really hope this gets fixed in the next release.
  72.  
  73. Despite this, I still believe GCC is the best compiler for the money.
  74.   
  75. -Charles Anstey
  76.  
  77.  
  78.