home *** CD-ROM | disk | FTP | other *** search
- 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
- From: mccauleyba@vax1.bham.ac.uk (Brian McCauley)
- Newsgroups: comp.std.c++
- Subject: Array anachronisms
- Message-ID: <1992Nov6.190320.1@vax1.bham.ac.uk>
- Date: 6 Nov 92 19:03:20 GMT
- Sender: usenet@rs6000.bham.ac.uk (USENET News Service)
- Organization: University of Birmingham
- Lines: 66
-
- Since array of objects requiring constructors can exit it follows that any
- C++ complier must contain the requisite logic for automatically inserting
- loops to handle arrays.
-
- Why must be live with the anachonistic restictions on the use of array
- variables?
-
- typedef T TA[5];
- TA a; //this correctly calls T::T() five times
- TA b=a; // should call T::T(const T&) five times
- TA b(a); // same as above
- a=b; // should call T::operator=(const T&) five times
-
- One can of course say...
- T b[5] = {a[0], a[1], a[2], a[3], a[4]};
- ...but this is ugly and its unlikely that the optimizer will spot that it's
- a loop. Also this won't work if the size of the array is not determined until
- compile time (for example as a macro passed in from the makefile).
-
- BTW I realise that that it may be possible to create a class TA with all these
- properties but I don't think that should be necessary.
-
- More controversially why can't we pass by arrays by value?
-
- typedef T TA[5];
- void foo(TA a); // should pass by value (and optionally give a warning)
- void foo(TA& a); // if I'd meant this I'd have said it
- void foo(T* const a); // if I'd meant this I'd have said it
-
- void foo(T a[]); // should do what is always has but give:-
- // "Warning: obsolete argument declaration syntax"
-
- There is some question as to how the complier should handle
- void foo(T a[5]);
- ...like `void foo(TA a)' a lot of old programs would fail
- ...like `void foo(T a[])' uglier but pragmatically better
-
- You may well ask, "How can...
- typedef T TA[5];
- void foo(TA a);
- ...and...
- void foo(T a[5]);
- ...mean something different?"
-
- OK I agree is ugly, but there is a precedent in the new operator.
-
- typedef T TA[5];
- TA* p1=new TA; // valid
- T* p2=new T[5]; // valid
- T* p3=new TA; // invalid
-
- I realise some old code would fail to compile correctly even with this
- distiction but this is a reasonable price to pay for removing the language's
- most unsightly wart.
-
- Note that none of what I've proposed inteferes in any way with the use of an
- array in a pointer context.
-
- If this question has been flogged to death in the past, I'm sorry.
-
- \\ ( ) NO BULLSHIT! from BAM (Brian McCauley)
- . _\\__[oo ============
- .__/ \\ /\@
- . l___\\ E-mail: B.A.McCauley@bham.ac.uk
- # ll l\\
- ###LL LL\\
-