Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

What you mean is that C++ doesn't have a way to (easily) let you check whether a given reference is null or not. int* a = NULL; int& b = *a; compiles and runs just fine.


> compiles and runs just fine.

For fairly low values of those. Creating a null reference is UB, your program is not legal at all.


If the compiler still accepts it, then that it belongs to the "UB" class of code is not much comfort.

The whole point is to NOT have it be accepted.


Sure, we're not supposed to do that. Sometimes it happens anyway, and the C++ compiler isn't much help in that case.


No the gp is correct, references in c++ can't be null. Your code invoked undefined behavior before you did anything with a reference, namely *a which is a null pointer dereference.


> namely *a which is a null pointer dereference.

Which is a textbook example of the null reference problem.

Edit: There may be some terminological confusion here: when programming language folks talk about "references", they include in that definition what C/C++ call "pointers". See for example the Wikipedia article, which gives as the C++ example not C++ references, but C++ pointers.

https://en.wikipedia.org/wiki/Reference_(computer_science)


The "null problem" is that a static language does a run-time check instead of a compile-time check. By the time the undefined behavior is invoked, compilation ended.


>Your code invoked undefined behavior before you did anything with a reference

Since nobody stopped you, the problem is still there.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: