why sizeof can't be overloaded?

M

Michael P. O'Connor

I believe the reason is because sizeof is a macro instead of a function.
thus this is done in the preprocessor and not in the compiler.
 
M

Michael P. O'Connor

sizeof being a macro..you must be kidding. It's an operator.

Thanks was always under the wrong impression there. And thanks for the
link, I will make sure to read it, even though you were giving the link to
the other person.
 
C

count zero

hi
I am a newbie in c++, help me!
thanks in much

Why on earth would you want to "overload" the sizeof operator?

Let me guess... Your father is also your uncle...
 
R

Rolf Magnus

Please always put your question into the posting's body, not just the
subject line.
why sizeof can't be overloaded?

I can see two reasons:
1st: sizeof is evaluated at compile-time, so you can use it in places where
a compile-time constant is required. If you overload it, that's not longer
possible.
2nd: What the heck would you need it for?
 
A

Andrew Koenig

I am a newbie in c++, help me!
thanks in much

Suppose sizeof could be overloaded. Can you give an example of a piece of
code that could usefully take advantage of that ability?
 
S

Siemel Naran

2nd: What the heck would you need it for?

One reason: to determine the memory held by the entire object, including
dynamic memory. For this one could add a virtual size function, though be
aware that the actual amount of dynamic memory used depends very much on
compiler and platform implementation details.

class Derived : pulic Base {
std::wstring s;
std::vector<double> v;
char ca[100];
public:
virtual size_t dynamic_size() const;
};

size_t Derived::dynamic_size() const {
size_t base = Base::dynamic_size() - sizeof(Base);
return
base
+ sizeof(Derived)
+ s.capacity()*sizeof(wchar_t)
+ v.capacity()*sizeof(double)
;
}

But I don't know how to get the dynamic memory of a std::deque, std::list,
or any of the other containers.
 
V

Victor Bazarov

Siemel Naran said:
2nd: What the heck would you need it for?

One reason: to determine the memory held by the entire object, including
dynamic memory. [...]

But sizeof is a compile-time operator. How the hell would it report the
dynamic memory? And another sentiment: what would be the use of that
reported size? You can't memcpy it anywhere, you can't pass that size
to the stream::write, because the memory is not contuguous.
 
S

Siemel Naran

Victor Bazarov said:
One reason: to determine the memory held by the entire object, including
dynamic memory. [...]

But sizeof is a compile-time operator. How the hell would it report the
dynamic memory? And another sentiment: what would be the use of that
reported size? You can't memcpy it anywhere, you can't pass that size
to the stream::write, because the memory is not contuguous.

Your points are valid. But to many people, especially those not too
familiar with the language, one of the uses of sizeof appears to be to
determine the actual size of an object, which could be useful for analyzing
memory usage of our program.
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Siemel said:
Your points are valid. But to many people, especially those not too
familiar with the language, one of the uses of sizeof appears to be to
determine the actual size of an object, which could be useful for
analyzing memory usage of our program.

The solution is that those people learn the language, not to change the
language.
 
J

Jonathan Turkanis

Siemel said:
Victor Bazarov said:
One reason: to determine the memory held by the entire object,
including dynamic memory. [...]

But sizeof is a compile-time operator. How the hell would it report
the dynamic memory? And another sentiment: what would be the use of
that reported size? You can't memcpy it anywhere, you can't pass
that size to the stream::write, because the memory is not contuguous.

Your points are valid. But to many people, especially those not too
familiar with the language, one of the uses of sizeof appears to be to
determine the actual size of an object, which could be useful for
analyzing memory usage of our program.

I doubt this notion of size is well-defined. Suppose you have a type T whose
instances store pointers to dynamically allocated objects of type D. Now, the
"real" size S of the instances should be something like the size discovered with
sizeof plus the size of the dynamically allocated objects. Now suppose you have
a vector of 100 instances of T. You'd think the size of the vector would be
about S * 100. But some instances of D may be shared among instances of T, and
there's no general way for vector to discover this.

Jonathan
 
O

Old Wolf

Siemel said:
Victor Bazarov said:
One reason: to determine the memory held by the entire object,
including dynamic memory. [...]

But sizeof is a compile-time operator. How the hell would it
report the dynamic memory?

In C99, sizeof can be a run-time operator. And there has been
talk of C++ adopting much of C99.
Your points are valid. But to many people, especially those not too
familiar with the language, one of the uses of sizeof appears to be to
determine the actual size of an object, which could be useful for analyzing
memory usage of our program.

string::string s[5];
s[0] = "hello";
foo(s, sizeof s / sizeof *s); // whoops
 
J

Jonathan Turkanis

Old said:
Siemel said:
Victor Bazarov said:
"Siemel Naran" <[email protected]> wrote...
One reason: to determine the memory held by the entire object,
including dynamic memory. [...]

But sizeof is a compile-time operator. How the hell would it
report the dynamic memory?

In C99, sizeof can be a run-time operator. And there has been
talk of C++ adopting much of C99.

I don't know of any plans to introduce variable length arrays into C++, do you?
Furthermore, making sizeof a runtime operation would interfere with common
type-traits and metaprogramming techniques.

Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top