logoalt Hacker News

StilesCrisis • today at 1:07 AM • 2 replies • view on HN

Don't forget * (true pointer) and & (called a reference, more honestly a pointer that can't be repointed, except with UB it can _totally_ get repointed).


Replies

Vaushite • today at 4:26 AM

Knowing the story behind a decision like that is worth a thousand spec clauses.

When the language got operator overloading there was the question of how to convert the arguments from the operator itself to its implementation. You wanted to write a+b, not &a+&b but most versions of operator+ were going to want const pointers for efficiency. One option was to make the & implicit, another was to introduce a type of argument that didn't need & but acted like a pointer. So references were born.

I ignore people who advise to use references when something can't be null. A null reference is UB just like a null pointer, it just shifts where the UB is, but the compiler is likely to produce the same code either way and the program is just as likely to crash. Instead, I use pointers to make it obvious that something is a pointer, and references when it should be invisible, just like with operator overloading.

➕ show 2 replies
senthil_rajasek • today at 1:33 AM

Made me LOL