No, compilers don't typically assume that. There's even commercial code going around with these checks, some not just asserts but actually logic in them. if (!this) { doSomeWork(); }
.
Although you couldn't reach a situation where this
would be NULL
without running into undefined behavior, if you're well aware of the implementation details then that's a check you can make; and yes, you're right, it can help in debugging.
I wouldn't put it everywhere though. Anywhere, for that matter. If this
is indeed NULL
, you'll likely get a crash later on when you're accessing some member. If you're not accessing any members, consider marking the method static
. It also bloats the code unnecessarily.