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

S

Shen Weihui

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

Juha Nieminen

Shen Weihui said:
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).
 
B

Balog Pal

Shen Weihui said:
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.
 
P

Paul

Shen Weihui said:
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..
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,481
Members
44,900
Latest member
Nell636132

Latest Threads

Top