upvote
Let's not vague post on HN. What's the problem with the above?
reply
The problem is the code unconditionally dereferences the pointer, which would be UB if it was a null pointer. This means it is legal to optimize out any code paths that rely on this, even if they occur earlier in program order.
reply
This is a very "Dr Dr it hurts when I do this" "Don't do that" one it must be said.
reply
I'm sorry, but what exactly is the problem with the code? I've been staring at it for quite a while now and still don't see what is counterintuitive about it.
reply
Depends on where you're coming from, but some people would expect it to enforce that the pointer is non-null, then proceed. Which would actually give you a guaranteed crash in case it is null. But that's not what it does in C++, and I could see it not being entirely obvious.
reply
Assert doesn't work like that in any language.
reply
It does in Rust: assert is always enabled, whereas the debug-only version is called debug_assert.

But yes, “assert” in most languages is debug-only.

reply
There's nothing wrong with it. It does exactly what you think it does when passed null.
reply
deleted
reply