diff --git a/include/memory b/include/memory index fe3523828..87436ac7d 100644 --- a/include/memory +++ b/include/memory @@ -621,7 +621,7 @@ inline _LIBCPP_INLINE_VISIBILITY _Tp* addressof(_Tp& __x) _NOEXCEPT { - return (_Tp*)&(char&)__x; + return (_Tp*)&reinterpret_cast(__x); } #if defined(_LIBCPP_HAS_OBJC_ARC) && !defined(_LIBCPP_PREDEFINED_OBJC_ARC_ADDRESSOF) diff --git a/test/utilities/memory/specialized.algorithms/specialized.addressof/addressof.pass.cpp b/test/utilities/memory/specialized.algorithms/specialized.addressof/addressof.pass.cpp index 3f1bef16a..e07bec4d0 100644 --- a/test/utilities/memory/specialized.algorithms/specialized.addressof/addressof.pass.cpp +++ b/test/utilities/memory/specialized.algorithms/specialized.addressof/addressof.pass.cpp @@ -19,8 +19,17 @@ struct A void operator&() const {} }; +struct nothing { + operator char&() + { + static char c; + return c; + } +}; + int main() { + { int i; double d; assert(std::addressof(i) == &i); @@ -30,4 +39,13 @@ int main() assert(std::addressof(*tp) == tp); assert(std::addressof(*ctp) == tp); delete tp; + } + { + union + { + nothing n; + int i; + }; + assert(std::addressof(n) == (void*)std::addressof(i)); + } }