-
July 10th, 2007, 10:19 AM
#1
Why "sizeof" is an operator, not a function?
Why "sizeof" is an operator, not a function?
-
July 10th, 2007, 10:30 AM
#2
Re: Why "sizeof" is an operator, not a function?
For one, if it were a function (and assuming it could operate as one), the value returned would only be available at runtime, not compile time. This would mean the compiler couldn't give you a constant integer value, and it would take more time.
As it is, sizeof is calculated at compile time, so there's no time occupied during runtime, it appears as a constant in the executable.
Ultimately, though, a function wouldn't have any way of determining the size of an object for which there is no RTTI (and even then only if RTTI were defined to include the size of the object, which I don't believe it is).
While it have been possible to create overloads for sizeof regarding built in primitives (returning a size for each one on that platform), this wouldn't work for structures and classes any better than a compile time operator (such functions would have to get the size value from somewhere, and that would probably be a compile time generated table, evaporating the function concept into a compile time concept during optimization).
-
July 10th, 2007, 10:30 AM
#3
Re: Why "sizeof" is an operator, not a function?
Why should it be a function? How would that be supposed to work/be better than is now?
B+!
'There is no cat' - A. Einstein
Use [code] [/code] tags!
Did YOU share your photo with us at CG Members photo gallery ?
-
July 10th, 2007, 10:34 AM
#4
Re: Why "sizeof" is an operator, not a function?
Originally Posted by JVene
Ultimately, though, a function wouldn't have any way of determining the size of an object for which there is no RTTI (and even then only if RTTI were defined to include the size of the object, which I don't believe it is).
Why is RTTI required? Are you saying you cannot know the size of a type at runtime?
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
July 10th, 2007, 12:12 PM
#5
Re: Why "sizeof" is an operator, not a function?
You don't need RTTI but sizeof can be used very cleverly in meta-programming.
-
July 10th, 2007, 01:30 PM
#6
Re: Why "sizeof" is an operator, not a function?
Originally Posted by warrener
Why "sizeof" is an operator, not a function?
- sizeof can take a type-id as an operand. Functions don't accept type-ids as arguments.
- For sizeof with an expression operand, the expression remains unevaluated. Functions calls evaluate their arguments.
Last edited by googler; July 11th, 2007 at 04:59 AM.
-
July 10th, 2007, 06:24 PM
#7
Re: Why "sizeof" is an operator, not a function?
Hey exterminator:
Why is RTTI required? Are you saying you cannot know the size of a type at runtime?
Well, in a way - what I was saying is that IF, as the OP posited, sizeof were a function, there would have to be a runtime determination made of the size, which isn't the way it operates in C++, and that would probably be part of RTTI. As it is, I don't think there is a runtime determination made, it's a compile time determination.
-
July 11th, 2007, 03:32 PM
#8
Re: Why "sizeof" is an operator, not a function?
Originally Posted by NMTop40
...but sizeof can be used very cleverly in meta-programming.
Do you happen to know if sizeof is guaranteed to be known by the C++ preprocessor?
Last edited by dude_1967; July 11th, 2007 at 03:32 PM.
Reason: clarity...
You're gonna go blind staring into that box all day.
-
July 11th, 2007, 11:34 PM
#9
Re: Why "sizeof" is an operator, not a function?
Originally Posted by dude_1967
Do you happen to know if sizeof is guaranteed to be known by the C++ preprocessor?
The preprocessor should not have anything to do with sizeof or templates.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
July 12th, 2007, 01:15 AM
#10
Re: Why "sizeof" is an operator, not a function?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|