testing `this' against 0 in non-static member function

Discussion in 'C++' started by Shen Weihui, Jun 20, 2011.

  1. Shen Weihui

    Shen Weihui Guest

    Is it valid that testing `this` against 0 in a non-static member
    function? Since I found that this kind of construction was totally
    eliminated by the compiler in the release build.

    In N3126 (9.3.2/1), it says, 'In the body of a non-static (9.3) member
    function, the keyword this is a prvalue expression whose value is the
    address of the object for which the function is called.'

    So I guess that the compiler assumes that the address of the object
    cannot be 0, so it's safe to optimize away the if expression.
     
    Shen Weihui, Jun 20, 2011
    #1
    1. Advertising

  2. Shen Weihui <> wrote:
    > Is it valid that testing `this` against 0 in a non-static member
    > function? Since I found that this kind of construction was totally
    > eliminated by the compiler in the release build.
    >
    > In N3126 (9.3.2/1), it says, 'In the body of a non-static (9.3) member
    > function, the keyword this is a prvalue expression whose value is the
    > address of the object for which the function is called.'
    >
    > So I guess that the compiler assumes that the address of the object
    > cannot be 0, so it's safe to optimize away the if expression.


    The address of an object can indeed never be null, thus if you try to
    call a method through a null pointer you have UB, which means that the
    compiler can do whatever it wants (such as optimize away checks against
    'this' being null).
     
    Juha Nieminen, Jun 20, 2011
    #2
    1. Advertising

  3. Shen Weihui

    Balog Pal Guest

    "Shen Weihui" <>

    > Is it valid that testing `this` against 0 in a non-static member function?


    Yes, it is legal. But pointless.

    > Since I found that this kind of construction was totally
    > eliminated by the compiler in the release build.


    Well done. That's what I was telling many years ago with little effect, as
    compilers had tendency to leave it at the time. this can not be 0 through
    any mandated behavior. To make it 0 you must dereference a 0 pointer
    upstream, that is undefined behavior. This gives the compiler the licence to
    eliminate the redundant check.

    > In N3126 (9.3.2/1), it says, 'In the body of a non-static (9.3) member
    > function, the keyword this is a prvalue expression whose value is the
    > address of the object for which the function is called.'
    >
    > So I guess that the compiler assumes that the address of the object
    > cannot be 0, so it's safe to optimize away the if expression.


    Yes. Current standard has tifferent wording but that means the same.
     
    Balog Pal, Jun 20, 2011
    #3
  4. Shen Weihui

    Paul Guest

    "Shen Weihui" <> wrote in message
    news:...
    > Is it valid that testing `this` against 0 in a non-static member
    > function? Since I found that this kind of construction was totally
    > eliminated by the compiler in the release build.
    >
    > In N3126 (9.3.2/1), it says, 'In the body of a non-static (9.3) member
    > function, the keyword this is a prvalue expression whose value is the
    > address of the object for which the function is called.'
    >
    > So I guess that the compiler assumes that the address of the object
    > cannot be 0, so it's safe to optimize away the if expression.
    >


    I think someplace in the C++ standard it states that calling a member
    function without it being invoked on an object is UB.

    The only way it could be invoked is with a function pointer.
    It would be possible to invoke such a function and have the this pointer set
    to null, details would be implementation specific.

    There is no point in this at all AFAICS because this function should be made
    static or as a non member function if it is to used , detached from an
    object..
     
    Paul, Jun 20, 2011
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Victor Bazarov
    Replies:
    0
    Views:
    1,366
    Victor Bazarov
    Jun 23, 2003
  2. slide_o_mix
    Replies:
    0
    Views:
    425
    slide_o_mix
    Oct 15, 2003
  3. Alex
    Replies:
    0
    Views:
    393
  4. dolphin
    Replies:
    3
    Views:
    1,349
    Pete Becker
    Dec 5, 2007
  5. ittium
    Replies:
    5
    Views:
    445
    88888 Dihedral
    Jan 12, 2012
Loading...

Share This Page