I think it's dangerous to overload the address-of operator &
, because it's easy to overlook the use of std::addressof()
in implementing templates that actually need it. Still, I'm wondering what would be a proper use case of overloading this operator. Particularly, is there any class (template) in the standard library that has done this? Finally, since std::addressof()
is introduced since C++11, is there any way to take the address of an object that has this overloading in C++03?

- 14,579
- 2
- 37
- 93
-
You might use the given implementation in C++03: [std::addressof](http://en.cppreference.com/w/cpp/memory/addressof) – Jarod42 Mar 09 '18 at 11:35
2 Answers
&
is often overloaded when building smart pointer classes, as it makes them easier to use: more often than not you want the bare managed pointer, not the address of the smart pointer object.
Neglecting the evolutionary backwater that was std::auto_ptr
, smart pointer classes such as std::unique_ptr
and std::shared_ptr
have been introduced into the standard since C++11. std::addressof
was introduced in that standard since taking the real address of an object is occasionally required.
A possible implementation of std::addressof
is trivial but is smelly due to the amount of casting required; a possible implementation is1
template< class T >
T* addressof(T& arg)
{
return reinterpret_cast<T*>(
&const_cast<char&>(
reinterpret_cast<const volatile char&>(arg)));
}
Other more exotic uses of overloading &
are for supporting EBNF grammar parsers. Boost Spirit does this with remarkable alacrity.
1 Quoting from http://en.cppreference.com/w/cpp/memory/addressof

- 231,907
- 34
- 361
- 483
-
3Neither `std::unique_ptr` nor `std::shared_ptr` overload the `&` operator so I don't see what your point is. – zett42 Mar 09 '18 at 12:05
-
1
No, the only way is the std::addressof()
in C++11 or later, or its implementation for earlier versions, see above:
template< class T >
T* addressof(T& arg)
{
return reinterpret_cast<T*>(
&const_cast<char&>(
reinterpret_cast<const volatile char&>(arg)));
}

- 7,050
- 2
- 37
- 76
-
I can't quote chapter and verse, but I *think* that is formally undefined behaviour. That is *why* `std::addressof` was added to the standard; most implementations can just use the code you have, but if it doesn't work, they have have to create a `__addressof__` keyword and have the compiler recognize it (or similar approach). This is the same reason `offsetof` is standardized. – Martin Bonner supports Monica Mar 09 '18 at 11:59
-
1you mean the implementation I provided is undefined? This is the actual implementation of addressof or very similar one! – Eduard Rostomyan Mar 09 '18 at 12:01
-
1Yes, I mean the code you provide is undefined. The standard library is part of the C++ implementation. As such, it is fine for it to rely on the behaviour of the particular compiler (remember "undefined" doesn't mean "won't work", it means "the standard doesn't guarantee it will work"). For those people trying to write portable code OTOH, relying on the behaviour of the compiler is a bad idea (particularly as, if the compiler behaviour changes, they can change the library to match). – Martin Bonner supports Monica Mar 09 '18 at 12:09
-
@MartinBonner If casting references behave the same as pointers (as I would intuit), it is well-defined. `offsetof` does more than casting, it also requires pointer arithmetic, which is undefined on anything other than arrays – Passer By Mar 09 '18 at 12:11
-
1It is noted however, that a `constexpr` version requires compiler support – Passer By Mar 09 '18 at 12:16