Why "sizeof" is an operator, not a function?
Printable View
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).
Why should it be a function? How would that be supposed to work/be better than is now?
Why is RTTI required? Are you saying you cannot know the size of a type at runtime? :)Quote:
Originally Posted by JVene
You don't need RTTI but sizeof can be used very cleverly in meta-programming.
Quote:
Originally Posted by warrener
- 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.
Hey exterminator:
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.Quote:
Why is RTTI required? Are you saying you cannot know the size of a type at runtime?
Do you happen to know if sizeof is guaranteed to be known by the C++ preprocessor?Quote:
Originally Posted by NMTop40
The preprocessor should not have anything to do with sizeof or templates.Quote:
Originally Posted by dude_1967