home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / cplus / 12939 < prev    next >
Encoding:
Internet Message Format  |  1992-08-26  |  2.0 KB

  1. Path: sparky!uunet!pipex!unipalm!uknet!mcsun!sunic!seunet!appli!niklas
  2. From: niklas@appli.se (Niklas Hallqvist)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: GCC2.2 doesn't inline template class functions. Normal?
  5. Message-ID: <2139@appli.se>
  6. Date: 26 Aug 92 21:53:05 GMT
  7. References: <ANSTEY.92Aug26014202@sunspot.clarkson.edu>
  8. Distribution: comp.lang.c++
  9. Organization: Applitron Datasystem AB, GOTHENBURG, SWEDEN
  10. Lines: 53
  11.  
  12. anstey@sun.soe.clarkson.edu (Charles Anstey) writes:
  13.  
  14. :While playing around with GCC2.2, I noticed that it does not inline
  15. :template class member functions.  Is this typical of all compilers for
  16. :reasons I do not know?  GCC2.2 will inline member functions of a
  17. :non-template class.
  18.  
  19. :The examples was quite simple.
  20. :template <class PT>
  21. :class S {
  22. :private:
  23. :  PT length;
  24. :public:
  25. :  S (int size):length(size) {};
  26. :  PT size () { return (length);};
  27. :};
  28.  
  29. :S.size() is not inlined.  If I remove the template PT and change it to 'int'
  30. :then it is inlined.
  31.  
  32. :Anyone know why?
  33.  
  34. This is from memory, I don't have access to my compiler for the moment
  35. so I can't test it.  If you instantiate a template class inside a function,
  36. the member functions gets queued up for compilation after the current one.
  37. If you refer to these members in this function you will get out-of-line
  38. calls, but calls in later functions will be inlined.  The reason for this
  39. is simply compiler simplicity.  When someone has the time, GCC might be
  40. changed to compile members directly during instantiation, pushing the
  41. current function's context on some stack.  Example:
  42.  
  43. int foo()
  44. {
  45.   S<int> s(1);
  46.   return s.size();    // Out of line
  47. }
  48.  
  49. int bar()
  50. {
  51.   S<int> s(1);
  52.   return s.size();    // Inline
  53. }
  54.  
  55. If this is wrong, try the same example with an explicit inline keyword
  56. on the size member function.  As I said, I don't remember the exact details,
  57. I will check tomorrow.
  58.  
  59. Niklas
  60. -- 
  61. Niklas Hallqvist    Phone: +46-(0)31-40 75 00
  62. Applitron Datasystem    Fax:   +46-(0)31-83 39 50
  63. Molndalsvagen 95    Email: niklas@appli.se
  64. S-412 63  GOTEBORG, Sweden     mcsun!seunet!appli!niklas
  65.