home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / cplus / 16010 < prev    next >
Encoding:
Text File  |  1992-11-09  |  1.8 KB  |  75 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!stanford.edu!leland.Stanford.EDU!mcgrant
  3. From: mcgrant@leland.Stanford.EDU (Michael Grant)
  4. Subject: typeof operator?
  5. Message-ID: <1992Nov10.092148.430@leland.Stanford.EDU>
  6. Followup-To: comp.lang.c++
  7. Summary: Good thing for templates to have
  8. Keywords: typeof templates
  9. Organization: Information Systems Laboratory, Stanford University
  10. Date: Tue, 10 Nov 92 09:21:48 GMT
  11. Lines: 62
  12.  
  13. Has anyone in the C++ committee considered inclusion of a typeof()
  14. operator into standard C++? It would certainly make for some very
  15. interesting templates.
  16.  
  17. The typeof() operator exists in GNU g++, but I'm not sure it can
  18. be used in the following manner. I would certainly hope it could.
  19.  
  20. Consider:
  21.  
  22. template<class T>
  23. class Foo {
  24.    T num;
  25.    typeof(abs(T)) len;
  26.    ...
  27. }
  28.  
  29. Now, if 'double abs(double)' were defined in-scope, Foo<double> would yield
  30.  
  31. class Foo<double> {
  32.     double num;
  33.     double len;
  34. ...
  35. }
  36.  
  37. But, if 'double abs(complex)' were defined in-scope, Foo<complex> would
  38. yield
  39.  
  40. class Foo<complex> {
  41.     complex num;
  42.     double len;
  43. ...
  44. }
  45.  
  46. Isn't that great? For readability's sake when writing member functions,
  47. it would make sense to do something like
  48.  
  49. template <class T>
  50. class Foo {
  51.     typedef typeof(abs(T)) absType;
  52.     complex num;
  53.     absType len;
  54. ...
  55. }
  56.  
  57. That way, inside member functions I can use Foo<T>::absType to refer to
  58. the type of that variable. And, in more complicated situations, it seems
  59. convenient for the template class to automatically track the changing
  60. output types of the functions it uses.
  61.  
  62. Any comments? Of course, this can be MANUALLY simulated with things like
  63.  
  64. template <class T,class absT>
  65. class Foo {
  66.     T num;
  67.     absT len;
  68. }
  69.  
  70. but it seems that this is a natural extension to parametrized types.
  71.  
  72. Michael C. Grant
  73. mcgrant@isl.stanford.edu
  74.  
  75.