Nonnull comparison warning for reference pointer

I wrote the following code:

void foo(int& x)
{
  bool b1 = (&x) == nullptr;
  bool b2 = &x == nullptr;
  int* ptr = &x;
  bool b3 = ptr == nullptr;
}

In the above code, I get the following errors:

For variable b1:

test.cpp: 5: 21: warning: argument nonnull 'x compared to NULL [-Wnonnull-compare]

For variable b2:

test.cpp: 6: 19: warning: the compiler may assume that the address "x will never be NULL [-Waddress]

test.cpp: 6: 19: warning: nonnull argument 'x compared to NULL [-Wnonnull-compare]

And no warnings for b3.

I understand why the compiler returned a warning that "x" would never be NULL. However, I do not understand why this warning was returned only for b2, not b1.

, , , '& x' . , T *, , T *?

, g++ (GCC) 6.2.1

.

+4
1

b2: "==" x - , , - , . , b1 - "" , , , . , , - , , , , ( , ).

0

Source: https://habr.com/ru/post/1654925/


All Articles