logoalt Hacker News

aw1621107 • yesterday at 9:27 AM • 1 reply • view on HN

Do you mind elaborating? Modern compilers will definitely optimize out checks for null references [0] (but probably aren't guaranteed to), so checking after the fact seems like an iffy proposition to me at best.

[0]: https://cpp.godbolt.org/z/6ze9ns7cz

    bool pointer(int* p) {
        return p == nullptr;
    }

    bool reference(int& r) {
        return &r == nullptr;
    }
Become

    pointer(int*):
            test    rdi, rdi
            sete    al
            ret

    reference(int&):
            xor     eax, eax
            ret
When compiled with Clang 23.1.0 and -O1.

Replies

ogoffart • today at 6:35 AM

I read the quote as "I have old code filled with bugs and UB. Using references don't magically solve the bugs"

➕ show 1 reply