Sounds like a trivial question judging from typical usage of &
operator, but is there a guarantee from the standard that address-of-operator cannot return nullptr
when trying to get an address?
Sounds like a trivial question judging from typical usage of &
operator, but is there a guarantee from the standard that address-of-operator cannot return nullptr
when trying to get an address?
2 Answers
Reset to default 7There is no guarantee that the operator&
can't return nullptr
, for instance by overloading this operator.
Here is a contrived example:
#include <iostream>
struct foo { foo* operator& () { return nullptr; } };
int main()
{
foo f;
if (&f == nullptr) { std::cout << "nullptr detected\n"; }
std::cout << "the true address is " << std::addressof(f) << "\n";
}
Demo
OP's question is specifically for &x
(as OP mentioned in a comment).
Can & (address-of-operator) ever return nullptr?
No for built-in types and no for user defined types, if operator &
is not overloaded. Otherwise, for user defined types, you can overload &
operator and then the behavior will be based on your implementation of operator&
, where you can return nullptr
as well.
From C++17 (8.3.1) Unary operators [emphasis mine]:
3 The result of the unary & operator is a pointer to its operand. The operand shall be an lvalue or a qualified-id. If the operand is a qualified-id naming a non-static or variant member m of some class C with type T, the result has type “pointer to member of class C of type T” and is a prvalue designating C::m. Otherwise, if the type of the expression is T, the result has type “pointer to T” and is a prvalue that is the address of the designated object (4.4) or a pointer to the designated function. [ Note: In particular, the address of an object of type “cv T” is “pointer to cv T”, with the same cv-qualification. — end note ] For purposes of pointer arithmetic (8.7) and comparison (8.9, 8.10), an object that is not an array element whose address is taken in this way is considered to belong to an array with one element of type T.
EDIT:
Below part is to justify the claim that address of operator &
cannot return nullptr
for a user defined type operand (that does not have overloaded &
operator).
Below references are from C++17.
From 7.11 Pointer conversions
1 A null pointer constant is an integer literal (5.13.2) with value zero or a prvalue of type std::nullptr_t. A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type and is distinguishable from every other value of object pointer or function pointer type. ....
From 8.10 Equality operators
2 If at least one of the operands is a pointer, pointer conversions (7.11), function pointer conversions (7.13), and qualification conversions (7.5) are performed on both operands to bring them to their composite pointer type (Clause 8). Comparing pointers is defined as follows:
(2.1) — If one pointer represents the address of a complete object, and another pointer represents the address one past the last element of a different complete object,88 the result of the comparison is unspecified.
(2.2) — Otherwise, if the pointers are both null, both point to the same function, or both represent the same address (6.9.2), they compare equal.
(2.3) — Otherwise, the pointers compare unequal.
A *obj1 = nullptr;
A obj2;
It is guaranteed that the expression obj1 == &obj2
will evaluate to false
.
operator&
, e.g.struct foo { foo* operator& () { return nullptr; } };
– edrezen Commented Nov 20, 2024 at 12:00std::addressof
which one should use if you want to be sure to get an address rather than whatever a silly overload may return. Are you asking whether the address of an object can benullptr
or about&x
specifically? – 463035818_is_not_an_ai Commented Nov 20, 2024 at 12:01