From -649581661367241998 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f78e5,197bf7393e7e5e7d X-Google-Attributes: gidf78e5,public From: hopper@omnifarious.mn.org (Eric M. Hopper) Subject: Re: overload operator sizeof Date: 1996/11/17 Message-ID: #1/1 X-Deja-AN: 197075301 references: x-original-date: 17 Nov 1996 11:15:21 -0600 organization: Omnifarious Software x-auth: PGPMoose V1.1 PGP comp.std.c++ newsgroups: comp.std.c++ d96-mst@nada.kth.se (Mikael Steldal) writes: > Has anyone considered if it would be good to allow overloading of > operator sizeof? I.e. > class foo { > // ... char* buffer; size_t bufsize; > public: > size_t operator sizeof(void) { return bufsize; } // ... > }; > For a string, the overloaded operator sizeof could return the same as > capacity(). > Then we could also allow the use of ::operator sizeof to always get > the real size of the object. I don't think this would add a great deal to the language. sizeof has historically been used for only two purposes. One is, finding out the size of a data structure so you could allocate memory for it. The other is compile-time determination of the number of elments in an array without going to the trouble of using a #define or const declaration. The use of sizeof that you propose doesn't leverage either of those prior uses to reduce the mental burden on the programmer. sizeof DOES have vague connations of 'actual storage used', which your new use does leverage, but the cost of the syntatic sugar would be too high. Most code currently using sizeof is critical code that heavily depends on sizeof's standard meaning in order to function. If someone adds a sizeof operator to a class, all that code breaks and has to be re-written to use ::operator sizeof. Also, adding a new overloadable operator increases the amount of things you have to think about and remember when you're reading code. One interesting change to sizeof that I could think of would be making it return the 'right' value for a derived class in the presence of RTTI, although I suspect that that proposal also has a number of problems. -- --Eric Hopper He who joyfully marches to music in rank and file has already earned my contempt. He has been given a large brain by mistake, since for him the spinal cord would suffice. - Mark Twain --- [ comp.std.c++ is moderated. To submit articles: try just posting with ] [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ] [ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ] [ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ] [ Comments? mailto:std-c++-request@ncar.ucar.edu ]