It&#x27;s not really surprising that invalid code doesn&#x27;t go along with compiler optimizations.<p>The correct way to deal with this here is probably either _NOT_ using nullptr, and instead using a special null object (similar to the end iterator); or using pointers instead of references (because you really want pointer behaviour).<p>Their fix is insane, and not something I&#x27;d want in production code. Unless you carefully vet your code and compiler version, you should never rely on undefined behaviour.<p>(There are some cases in the c standard that technically undefined but all compiler vendors have essentially agreed to handle in the same way; so those are fine. IIRC unions are a common example.)