by aw1621107 · 1w ago · 0 comments
I think the difference is that with references you can only perform such a check before you create the reference. Pretty sure optimizers can and will elide checks for null references.
As someone who had the displeasure of maintaining a decades-old legacy project that had references initialized and assigned with NULL, I don't agree.
[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.