home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / cplus / 16739 < prev    next >
Encoding:
Text File  |  1992-11-22  |  2.6 KB  |  92 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!stanford.edu!EE.Stanford.EDU!sierra!mcgrant
  3. From: mcgrant@rascals.stanford.edu (Michael C. Grant)
  4. Subject: Re: template member func:  class X { template<class T> f(T& x) {...}}
  5. In-Reply-To: gs4t@virginia.edu's message of 22 Nov 92 21:03:14 GMT
  6. Message-ID: <MCGRANT.92Nov22175156@rascals.stanford.edu>
  7. Sender: usenet@EE.Stanford.EDU (Usenet)
  8. Organization: Information Systems Laboratory, Stanford University
  9. References: <mg.722310420@elan> <1992Nov22.210314.18135@murdoch.acc.Virginia.EDU>
  10. Date: 22 Nov 92 17:51:56
  11. Lines: 79
  12.  
  13. In article <1992Nov22.210314.18135@murdoch.acc.Virginia.EDU>
  14. gs4t@virginia.edu (Gnanasekaran Swaminathan) writes:
  15.  
  16.    mg@elan (Michael Golan) writes:
  17.    : Why isnt this allowed?
  18.    : 
  19.    : class File {
  20.    :     binwrite(void *buf,size_t sz) {...} // write buf of length sz 
  21.    : public:
  22.    :     template<class T> write(T& buf) { binwrite(buf,sizeof(T)); }
  23.    : };
  24.    Because there is an alternate way to do the same.
  25.    template <class T>
  26.    class File {
  27.        void binwrite(void* buf, int sz) {}
  28.    public:
  29.        void write(T* b) { binwrite(b, sizeof(T)); }
  30.    };
  31.    main()
  32.    {
  33.        File<int> f;
  34.        int i = 0;
  35.        f.write(i);
  36.    }
  37.  
  38. That ISN'T the same thing. What if I wanted to do this:
  39.  
  40. main() {
  41.   File* f;
  42.   int i = 10;
  43.   double j = 4;
  44.   ...
  45.   f.write(i);
  46.   f.write(j);
  47. }
  48.  
  49. The benefit to allowing this is that write(T& buf) could then be inlined
  50. for every data type. I believe a superior example is this:
  51.  
  52. template <class T>
  53. class Matrix {
  54. // just guess how it's defined!
  55. public:
  56.   template <T (*func)(const T&)> Matrix& apply( void ) {
  57.        for ( register int i = 0; i < totalLength ; i++ )
  58.            data[i] = (*func)(data[i]);
  59.        return *this;
  60.   }
  61.   Matrix& apply( T (*func)(const T&) ) { ...non-templated form... }
  62.   Matrix operator-( void ) const { return Matrix(*this).apply<&operator->(); }
  63.   // questionable syntax?
  64. }
  65.  
  66. Why is this useful? Because some computers have inlinable code for some
  67. of the standard floating-point functions such as fabs(), sqrt(), etc. So,
  68. I would hope and expect that a compiler could take
  69.  
  70. main() {
  71.   Matrix<double> m(50,50);
  72.   ...
  73.   m.apply<&fabs>();
  74.   m.apply<&sqrt>();
  75. }
  76.  
  77. and properly inline the code. (alternatively, I could have written an
  78. inlined function myself...) I've also wanted to template conversions
  79. from one Matrix type to another, i.e.
  80.  
  81. template <class T,class U>
  82. Matrix<T>::Matrix( const Matrix<U>& m ) { ... }
  83.  
  84. and let compiler errors and the like reject conversions that aren't
  85. automatic.
  86.  
  87. Mike
  88.  
  89. --
  90. -----
  91. "Long hair, short hair--what's the difference once the head's blowed off?" (?)
  92.