I've written following code:
void foo(int& x)
{
bool b1 = (&x) == nullptr;
bool b2 = &x == nullptr;
int* ptr = &x;
bool b3 = ptr == nullptr;
}
For the code above, I'm getting following errors:
For b1 variable:
test.cpp:5:21: warning: nonnull argument ‘x’ compared to NULL [-Wnonnull-compare]
For b2 variable:
test.cpp:6:19: warning: the compiler can assume that the address of ‘x’ will never be NULL [-Waddress]
test.cpp:6:19: warning: nonnull argument ‘x’ compared to NULL [-Wnonnull-compare]
And no warning for b3.
I understand why compiler returned a warning, that 'x' will never be NULL. However I don't understand why this warning has been returned only for the b2, and not a b1.
Also, I don't really understand what does it mean, that '&x' is a nonnull argument. Does it mean, that pointer to reference is not of type T*, but it has a special type, which is cast-able to T*?
If it does matter, I use compiler g++ (GCC) 6.2.1
Thanks for all your hints.