Is there any section in the C++ standard the shows that NULL references are ill-formed?
I am trying to show my lecturer (this is for an assignment for which I am being graded) that the following expression is undefined behaviour:
AClass* ptr = 0;
AClass& ali = *ptr;
std::cout << "\n" << (AClass*) &ali << '\n';
The violations I see, is dereferencing of a null pointer, and then referencing a null reference. In an a program he is using as a correct example, he is comparing the return of the dereferenced pointer reference:
(AClass*) &ali != (AClass*) 0
As a test for an objects validity. I saw this as completely undefined behavior; I want to find a quote from the standard that is a bit more concrete for my explanation.
If I’m wrong, then please show where I have made an error.
§8.5.3/1: “A variable declared to be a T&, that is “reference to type T” (8.3.2), shall be initialized by an object, or function, of type T or by an object that can be converted into a T.”
The code above does not initialize the reference with an object or function of type T or an object that can be converted to T. The violates the “shall”. At that point, the only room for question is whether it’s undefined behavior, or whether this qualifies as a diagnosable rule, in which case the compiler would be required to give an error message. Either way, it’s clearly wrong though.