Topic: overload operator sizeof
Author: hopper@omnifarious.mn.org (Eric M. Hopper)
Date: 1996/11/23 Raw View
Chelly Green <chelly@eden.com> writes:
> Eric M. Hopper wrote:
> > 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.
>
> Might be useful, i.e. typeid (obj_ref).size(), like typeid
> (obj_ref).name(). But then you'd need to get a pointer to the beginning
> of the object. Once you have that and the size, then what? What useful
> operations can you perform?
Mostly garbage collection and memory management type things.
*chuckle* There's probably a better way to accomplish that goal though.
You might also make a stab at implementing some hokey
persistence mechanism this way, but IMHO, you'd just be asking for
trouble if you needed a dynamic size to do memory image style
persistence. If you don't have enough type information to KNOW the
size, you aren't storing enough type information to reliably reconstruct
the object later.
Oh, well, :-)
--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 ]
Author: d96-mst@nada.kth.se (Mikael Steldal)
Date: 1996/11/17 Raw View
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.
---
[ 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 ]
Author: hopper@omnifarious.mn.org (Eric M. Hopper)
Date: 1996/11/17 Raw View
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 ]
Author: Chelly Green <chelly@eden.com>
Date: 1996/11/19 Raw View
Eric M. Hopper wrote:
>
> 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.
template<class T>
inline size_t size_of( T const& ) { return sizeof (T); }
class foo {
size_t bufsize;
public:
friend size_t size_of( foo const& foo ) { return foo.bufsize; }
};
foo x;
int i;
size_t s = size_of( x );
s = size_of( i );
Will that work? Just have to be darn sure you are specializing the
template for uses of foo, otherwise the template will be used for foo,
which would be bad.
> I don't think this would add a great deal to the language.
I agree.
...
> 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.
Actual storage required to hold an object of type T, not necessarily the
storage required internally by type T. Under the redefined sizeof (), I
guess a char* should return the length of the string, right? Or maybe
that + the space required for the pointer? :-) Kinda makes sizeof ()
mean "something related to the storage required". Not very useful.
...
> 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.
Might be useful, i.e. typeid (obj_ref).size(), like typeid
(obj_ref).name(). But then you'd need to get a pointer to the beginning
of the object. Once you have that and the size, then what? What useful
operations can you perform?
--
Chelly Green | chelly@eden.com | C++ - http://www.eden.com/~chelly
---
[ 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 ]