Re: Overloading sizeof operator
* Faisal:
On Jun 18, 11:58 pm, "Alf P. Steinbach" <al...@start.no> wrote:
* Lilith:
On Wed, 18 Jun 2008 09:30:33 -0700 (PDT), Faisal <faisal...@gmail.com>
wrote:
Hi,
Why C++ doesn't allow overloading of size of operator. I think it
would be much handy to check the type userdefined types.
For eg. In my project, I've some structures which contains dynamic
data( pointers). So if i have some way to overload the sizeof operator
I can calculate the exact size and return.
Is it because it's more like a compile time operator than a runtime?
No, it's because some operations need to be basic and not have their meaning
changed by programmers, so that it's possible to always get a handle on things.
Hi Alf,
Could you please elaborate this little more?
I couldn't get the actual problem.
Huh.
OK.
Consider implementing a class like std::vector. The reserve(n) member function
allocates (if necessary) a buffer large enough to hold n instances of the
element type T, without initializing the elements in the part that is beyond the
previous size. To obtain such uninitialized storage it needs to determine the
direct size of a T instance, not its total size when dynamic memory it "owns" is
included, or whatever the programmer might have redefined sizeof to mean. In
short, it needs a non-modified sizeof, a guaranteed basic sizeof. Which
guarantee it wouldn't have if sizeof could be overloaded.
Or you might consider a dirty implementation-specific trick to determine whether
a non-final class is polymorphic, by measuring the size of a derived class with
at least one virtual member function.
That wouldn't work if sizeof could be redefined (happily such trick won't be
needed in C++0x where you can get that information directly).
Now you might say, hey, no problem, anyone who needs the /real/ basic size can
just define some implementation-specific function like
template< typename T >
size_t realSizeOf() { return (char*)( ((T*)0)+1 ) - (char*)(0); }
which is formally UB but could conceivably be well-defined for a given compiler.
But then realSizeOf plays the role that sizeof does now, only with less of a
guarantee against redefinition, and in addition the definitions of pointer
arithmetic and array indexing would have to be reworded to avoid use of possibly
redefined sizeof, meaning a bit more contorted language in the standard.
Without some guaranteed not redefinable basic mechanisms we'd be lost. The OP
can just provide his own size() operation or whatever, no need to redefine the
built-in basic one, which should not have its meaning redefined ever.
Cheers, & hth.,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Please don't quote signatures.
Cheers, & hth.,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?