home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / std / cplus / 1505 < prev    next >
Encoding:
Internet Message Format  |  1992-11-08  |  2.6 KB

  1. Path: sparky!uunet!charon.amdahl.com!pacbell.com!sgiblab!zaphod.mps.ohio-state.edu!saimiri.primate.wisc.edu!ames!agate!doc.ic.ac.uk!uknet!fulcrum!bham!bhamvx!mccauleyba
  2. From: mccauleyba@vax1.bham.ac.uk (Brian McCauley)
  3. Newsgroups: comp.std.c++
  4. Subject: Array anachronisms
  5. Message-ID: <1992Nov6.190320.1@vax1.bham.ac.uk>
  6. Date: 6 Nov 92 19:03:20 GMT
  7. Sender: usenet@rs6000.bham.ac.uk (USENET News Service)
  8. Organization: University of Birmingham
  9. Lines: 66
  10.  
  11. Since array of objects requiring constructors can exit it follows that any
  12. C++ complier must contain the requisite logic for automatically inserting
  13. loops to handle arrays.
  14.  
  15. Why must be live with the anachonistic restictions on the use of array
  16. variables?
  17.  
  18. typedef T TA[5];
  19. TA a; //this correctly calls T::T() five times
  20. TA b=a; // should call T::T(const T&) five times
  21. TA b(a); // same as above
  22. a=b; // should call T::operator=(const T&) five times
  23.  
  24. One can of course say...
  25. T b[5] = {a[0], a[1], a[2], a[3], a[4]};
  26. ...but this is ugly and its unlikely that the optimizer will spot that it's
  27. a loop. Also this won't work if the size of the array is not determined until
  28. compile time (for example as a macro passed in from the makefile).
  29.  
  30. BTW I realise that that it may be possible to create a class TA with all these
  31. properties but I don't think that should be necessary.
  32.  
  33. More controversially why can't we pass by arrays by value?
  34.  
  35. typedef T TA[5];
  36. void foo(TA a); // should pass by value (and optionally give a warning)
  37. void foo(TA& a); // if I'd meant this I'd have said it
  38. void foo(T* const a); // if I'd meant this I'd have said it
  39.  
  40. void foo(T a[]); // should do what is always has but give:-
  41.   // "Warning: obsolete argument declaration syntax"
  42.  
  43. There is some question as to how the complier should handle
  44. void foo(T a[5]);
  45.  ...like `void foo(TA a)' a lot of old programs would fail
  46.  ...like `void foo(T a[])' uglier but pragmatically better
  47.  
  48. You may well ask, "How can...
  49.  typedef T TA[5];
  50.  void foo(TA a);
  51. ...and...
  52.  void foo(T a[5]);
  53. ...mean something different?"
  54.  
  55. OK I agree is ugly, but there is a precedent in the new operator.
  56.  
  57. typedef T TA[5];
  58. TA* p1=new TA; // valid
  59. T* p2=new T[5]; // valid
  60. T* p3=new TA; // invalid
  61.  
  62. I realise some old code would fail to compile correctly even with this
  63. distiction but this is a reasonable price to pay for removing the language's
  64. most unsightly wart.
  65.  
  66. Note that none of what I've proposed inteferes in any way with the use of an
  67. array in a pointer context.
  68.  
  69. If this question has been flogged to death in the past, I'm sorry.
  70.  
  71.     \\   ( )  NO BULLSHIT! from BAM (Brian McCauley)          
  72.  .  _\\__[oo  ============
  73. .__/  \\ /\@
  74. .  l___\\     E-mail: B.A.McCauley@bham.ac.uk
  75.  # ll  l\\ 
  76. ###LL  LL\\
  77.