How would the standard allow indirection through a null pointer?
EDIT: This is related to active issue 232 on the C++ Standard Core Language Active Issues
So the real question is how would the standard allow indirection through a null pointer?
Consider the following code:
struct a { int x; };
struct b { int y; };
struct c: a, b { };
b *f(c *pointer_to_c) { return pointer_to_c; }
f(...)
has to test if pointer_to_c
is NULL
and return NULL
if it is. (A NULL
pointer is always a NULL
pointer, no matter how you cast it.)
Now, consider the following:
b *f_ref(c &reference_to_c) { return &reference_to_c; }
First question: does f_ref
need to check if &reference_to_c
is NULL
?
Second question: if C++ eventually defines the behavior of indirection through a null pointer, does this mean f_ref
has to check for NULL
? (I suppose the answer depends on what the standard allows, i.e., if it says "casting a NULL reference to a base class is undefined for multiple inheritance", then the answer is clearly no.)
For one last puzzle, now consider this:
const b *f_const_ref(const c &reference_to_c) { return &reference_to_c; }
开发者_如何学运维
Does f_const_ref
have to check &reference_to_c
to NULL
? Incidentally, with VC++ 2010, all three functions got optimized to f
, that is, all three functions test the input against NULL
.
So given that you cannot pass a null reference in a well-defined program, does that mean that if the C++ standard eventually allows indirection through a null pointer it cannot result in an expression or subexpression that would otherwise bind to a null pointer and create a null reference? In other words, if the standard allows indirection through a null pointer, what are the various ways they could allow it?
De-referencing a NULL pointer is invalid (i.e. Undefined behavior).
Thus it is never possible to get a reference from a NULL (in valid code).
Thus any reference you get will never be NULL and thus you do not need to check for it.
First question: does f_ref need to check if &reference_to_c is NULL?
No.
Second question: if C++ eventually defines the behavior of indirection through a null pointer does this mean f_ref has to check for NULL?
It does. Its undefined behavior. Thus any further speculation is worthless.
For one last puzzle, Does f_const_ref have to check &reference_to_c to NULL?
No.
If your reference in f_ref
or f_const_ref
is NULL, you are deep in the realm of undefined behavior.
This is undefined because the only way to get a NULL reference is to dereference a NULL pointer. So the below is undefined, though on g++ 4.5.2 it will output 0.
#include <iostream>
struct C {};
C* foo(C& r) { return &r; }
int main() {
C* c = NULL;
std::cout << foo(*c) << std::endl;
return 0;
}
C++ does not allow this. The reference to NULL
is illegal and standards-wise will result in undefined behaviour.
Having said that, I've never seen a compiler do anything other than your expectation.
&reference_to_c
can never be NULL. In a function parameter, a reference has to be initialized with some valid object. So &reference_to_c
cannot be NULL.
精彩评论