From c76d2bda6ffc092b4d0471dba00e2f46d2040638 Mon Sep 17 00:00:00 2001 From: Howard Hinnant Date: Tue, 16 Apr 2013 17:27:56 +0000 Subject: [PATCH] addressof misbehaving for type with an implicit conversion operator to char&. This fixes http://llvm.org/bugs/show_bug.cgi?id=15754 llvm-svn: 179608 --- libcxx/include/memory | 2 +- .../specialized.addressof/addressof.pass.cpp | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/libcxx/include/memory b/libcxx/include/memory index fe3523828b01..87436ac7d91a 100644 --- a/libcxx/include/memory +++ b/libcxx/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/libcxx/test/utilities/memory/specialized.algorithms/specialized.addressof/addressof.pass.cpp b/libcxx/test/utilities/memory/specialized.algorithms/specialized.addressof/addressof.pass.cpp index 3f1bef16a3f6..e07bec4d0a47 100644 --- a/libcxx/test/utilities/memory/specialized.algorithms/specialized.addressof/addressof.pass.cpp +++ b/libcxx/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)); + } }