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

  1. Path: sparky!uunet!charon.amdahl.com!pacbell.com!ames!agate!doc.ic.ac.uk!uknet!fulcrum!bham!bhamvx!mccauleyba
  2. From: mccauleyba@vax1.bham.ac.uk (Brian McCauley)
  3. Newsgroups: comp.lang.c++
  4. Subject: Inheriting from inbuilt types (was: Re: Two things...)
  5. Message-ID: <1992Nov17.193052.1@vax1.bham.ac.uk>
  6. Date: 17 Nov 92 19:30:52 GMT
  7. References: <MCGRANT.92Nov15134127@rascals.stanford.edu> <1992Nov16.202036.1@vax1.bham.ac.uk> <Bxu1Gp.M9o@fiu.edu>
  8. Sender: usenet@rs6000.bham.ac.uk (USENET News Service)
  9. Organization: University of Birmingham
  10. Lines: 105
  11.  
  12. In article <Bxu1Gp.M9o@fiu.edu>, feathers@serss0 (Michael Feathers) writes:
  13. > In article <1992Nov16.202036.1@vax1.bham.ac.uk> mccauleyba@vax1.bham.ac.uk (Brian McCauley) writes:
  14. >>...
  15. >>There is a presedent for using the token `class' in this way:
  16.                                                       ^^^^^^^^ (to mean `type')
  17. >>  `template<class T>' "class" meaning "T is a symbol of type class"
  18. >>...
  19. >...
  20. > Why does the syntax for a template require the class keyword when templates
  21. > are instantiable to the predefined types of C++ (int, double, etc.,.)?
  22. As I said class here is used to mean type, there are such things as
  23. template arguments with types other than `class'.
  24.  
  25. template <class T,int sz> class buffer { T v[sz]; /*...*/ };
  26.      (from "The C++ Prog Lang Ed.2" p279)
  27. > I suspect the reason is to disambiguate expressions and class/type names
  28. > as parameters to templates.  However, it is a wonderful start towards 
  29. > perhaps making the predefined types of C++ behave as classes.  It would
  30. > be nice to be able to inherit from int or float, for instance.  Anyone
  31. > who would want to do something like this today needs to make a class
  32. > with an int or float member.  Most compilers that I've run into still
  33. > generate code which accesses the single member like a struct even though
  34. > it is at offset zero in the struct.
  35. > What would we do for syntax?  How about this:
  36. >  
  37. > class Integer : public int
  38. > {
  39. >     public:
  40. >         operator int () { return *this; }
  41. > };
  42. Actually you wouldn't need to specify this as conversion to a base class
  43. is automatic. 
  44. > *this would be the access for the int portion of an Integer.  Note that
  45. > if there were additional members in class Integer, an int cast would have
  46. > the effect of "object-slicing" *this to only the int portion.
  47. > I'm not sure if such lvalue casting is now allowed. 
  48. Yes it implicitly calls the aforementioned compiler supplied conversion
  49. operator. I often have member functions that `return *this' and actually
  50. involve a type conversion. Unfortunately the example you cite would
  51. be an infinite recursion! 
  52. > All of this is off
  53. > the top of my head, but I think that making the predefined types into 
  54. > classes would certainly help the orthogonality of the language.
  55. >  
  56. > The syntax of templates certainly suggests that predefined types are
  57. > sort of like classes, doesn't it?
  58. Yes I've often sat in the bath contenplating if I would like to see
  59. inheritance from inbuilt types. Certianly it would be nice but I think
  60. I have a way of getting the same effect without the need for changing the
  61. language. The fact that conversions between inbuilt types are handled
  62. differently from conversions between user defined types will always
  63. mean that you could never inherit all the properties of an inbuilt type.
  64.  
  65. I  propose a standard library header file <intypes.h>
  66.  
  67. // intypes.h
  68.  
  69. template <class T> class InType {
  70.   T value;
  71. public:
  72.   operator T() {return value;}
  73.   InType(T v) : value(t) {}
  74. };
  75.  
  76. template <class T> class Numeric : public InType<T> {
  77.  
  78.   T& operator+=(const T& that) {value+=that.value; return *this;}
  79.   T operator+(T that) {that.value+=value; return that;}
  80.    // and so on for all the other operators valid on float
  81. };
  82. template <class T> class Integral<T> : public Numeric <T> {
  83. public:
  84.   // all the operators valid for int but not float
  85. };
  86. template <class T> class Pointer : Intype <T*> {
  87. public:
  88.   //all the operators valid for pointers
  89. }
  90.  
  91. template <class T,int size> class Array {
  92.   T value[size];
  93. public:
  94.   operator Pointer<T> () { return value; }
  95.   operator T* { return value; }
  96.    // and so on a long way
  97. };
  98.  
  99. template <class T> class Enum<T> { /*...*/ }
  100.  
  101. OK so the actual class names may need further consideration. (I'd never
  102. actually written this idea down before today).
  103.  
  104. You can now effectively inherit from int by inheriting from
  105. Integral<int> or from long by inheriting from Integral<long>.
  106. -- 
  107.     \\   ( )    No Bullshit!     |  Email: B.A.McCauley@bham.ac.uk   
  108.  .  _\\__[oo        from         |  Voice: +44 21 471 3789 (home)
  109. .__/  \\ /\@    /~)  /~[   /\/[  |    Fax: +44 21 625 2175 (work)
  110. .  l___\\      /~~) /~~[  /   [  |  Snail: 197 Harborne Lane,
  111.  # ll  l\\    ~~~~ ~   ~ ~    ~  |         Birmingham, B29 6SS, UK
  112. ###LL  LL\\   (Brian McCauley)   |   ICBM: 52.5N 1.9W
  113.