How can I reliably get an object’s address when operator& is overloaded?

Use std::addressof.

You can think of it as doing the following behind the scenes:

  1. Reinterpret the object as a reference-to-char
  2. Take the address of that (won’t call the overload)
  3. Cast the pointer back to a pointer of your type.

Existing implementations (including Boost.Addressof) do exactly that, just taking additional care of const and volatile qualification.

Leave a Comment