CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Sep 2006
    Posts
    141

    Why "sizeof" is an operator, not a function?

    Why "sizeof" is an operator, not a function?

  2. #2
    Join Date
    Nov 2006
    Posts
    1,611

    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).

  3. #3
    Join Date
    Dec 2004
    Location
    Poland
    Posts
    1,165

    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 ?

  4. #4
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: Why "sizeof" is an operator, not a function?

    Quote 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?

  5. #5
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Why "sizeof" is an operator, not a function?

    You don't need RTTI but sizeof can be used very cleverly in meta-programming.

  6. #6
    Join Date
    Dec 2005
    Posts
    642

    Arrow Re: Why "sizeof" is an operator, not a function?

    Quote Originally Posted by warrener
    Why "sizeof" is an operator, not a function?
    1. sizeof can take a type-id as an operand. Functions don't accept type-ids as arguments.
    2. 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.

  7. #7
    Join Date
    Nov 2006
    Posts
    1,611

    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.

  8. #8
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557

    Re: Why "sizeof" is an operator, not a function?

    Quote 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.

  9. #9
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: Why "sizeof" is an operator, not a function?

    Quote 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.

  10. #10
    Join Date
    Aug 2006
    Posts
    16

    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
  •  





Click Here to Expand Forum to Full Width

Featured