473,407 Members | 2,629 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

why sizeof can't be overloaded?

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

lokicer
Jul 23 '05 #1
13 2232
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.
Jul 23 '05 #2

"Michael P. O'Connor" <mp**@mikeoconnor.net> wrote in message
I believe the reason is because sizeof is a macro instead of a function.
sizeof being a macro..you must be kidding. It's an operator.
thus this is done in the preprocessor and not in the compiler.


To OP: http://www.research.att.com/~bs/bs_f...l#overload-dot

Sharad
Jul 23 '05 #3
On Sat, 05 Mar 2005 12:14:46 +0530, Sharad Kala wrote:
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.
Jul 23 '05 #4
On Sat, 5 Mar 2005 14:29:24 +0800, "Lokicer" <lo*****@163.com> wrote:
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...
Jul 23 '05 #5
Please always put your question into the posting's body, not just the
subject line.

Lokicer wrote:
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?

Jul 23 '05 #6
"Lokicer" <lo*****@163.com> wrote in message
news:d0***********@mail.cn99.com...
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?
Jul 23 '05 #7
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:d0*************@news.t-
why sizeof can't be overloaded?
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.
Jul 23 '05 #8
"Siemel Naran" <Si*********@REMOVE.att.net> wrote...
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:d0*************@news.t-
> why sizeof can't be overloaded?

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.
Jul 23 '05 #9
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:UNudneT8QfdVzrffRVn-
"Siemel Naran" <Si*********@REMOVE.att.net> 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? 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.
Jul 23 '05 #10
Siemel Naran wrote:
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.

--
Salu2
Jul 23 '05 #11
Siemel Naran wrote:
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:UNudneT8QfdVzrffRVn-
"Siemel Naran" <Si*********@REMOVE.att.net> 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? 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
Jul 23 '05 #12
Siemel Naran wrote:
"Victor Bazarov" <v.********@comAcast.net> wrote:
"Siemel Naran" <Si*********@REMOVE.att.net> 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.
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

Jul 23 '05 #13
Old Wolf wrote:
Siemel Naran wrote:
"Victor Bazarov" <v.********@comAcast.net> wrote:
"Siemel Naran" <Si*********@REMOVE.att.net> 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
Jul 23 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Nitin Bhardwaj | last post by:
Thanx in advance for the response... I wanna enquire ( as it is asked many a times in Interviews that i face as an Engg PostGraduate ) about the overloading capability of the C++ Language. ...
70
by: Roy Yao | last post by:
Does it mean "(sizeof(int))* (p)" or "sizeof( (int)(*p) )" ? According to my analysis, operator sizeof, (type) and * have the same precedence, and they combine from right to left. Then this...
1
by: masood.iqbal | last post by:
I have a few questions regarding overloaded typecast operators and copy constructors that I would like an answer for. Thanks in advance. Masood (1) In some examples that I have seen...
4
by: masood.iqbal | last post by:
Please help me with this doubt that I have regarding overloaded operators. Sometimes they are member functions and sometimes they are friends (e.g. see the code snippet from Stroustrup, Second...
3
by: Christof Warlich | last post by:
Hi, I'm trying to build an _efficient_, _purely_ _abstract_ API for inter process(or) communication, _completely_ hiding any implementation details. The core components are "Buffer", "Address"...
44
by: bahadir.balban | last post by:
Hi, What's the best way to implement an overloaded function in C? For instance if you want to have generic print function for various structures, my implementation would be with a case...
3
by: cybertof | last post by:
Hello, Is it possible in C# to have 2 overloaded functions with - same names - same parameters - different return type values If no, is it possible in another language ?
58
by: Nishu | last post by:
Hi All, When I run the below program in MSVC, I get the output as 1 4 Could you tell me why sizeof 'A' is taken as 4? Is it standard defined or compiler specific? Thanks, Nishu...
9
by: Faisal | last post by:
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.