diff --git a/include/mimalloc.h b/include/mimalloc.h index 22e54a70..b63150f6 100644 --- a/include/mimalloc.h +++ b/include/mimalloc.h @@ -20,7 +20,7 @@ terms of the MIT license. A copy of the license can be found in the file #else #define mi_attr_noexcept throw() #endif - #if __cplusplus > 201703L //C++20 + #if (__cplusplus > 201703L) //C++20 #define mi_attr_nodiscard_if_cpp20 [[nodiscard]] #else #define mi_attr_nodiscard_if_cpp20 @@ -368,6 +368,7 @@ mi_decl_export void* mi_new_reallocn(void* p, size_t newcount, size_t size) mi_a #if (__cplusplus >= 201103L) || (_MSC_VER > 1900) // C++11 #include // std::true_type #include // std::forward +#include // std::addressof #endif template struct mi_stl_allocator { @@ -406,8 +407,15 @@ template struct mi_stl_allocator { #endif size_type max_size() const mi_attr_noexcept { return (std::numeric_limits::max() / sizeof(value_type)); } - pointer address(reference x) const { return &x; } - const_pointer address(const_reference x) const { return &x; } + #if (__cplusplus >= 201103L) || (_MSC_VER > 1900)) // C++11 + pointer address(reference x) const mi_attr_noexcept { return std::addressof(x); } + const_pointer address(const_reference x) const mi_attr_noexcept { return std::addressof(x); } + #else + // Oversimplified implementation as presented here: + // This is also used as a fallback by libcxx, and as the sole implementation by libstdcxx, so should be good enough for us + pointer address(reference x) const mi_attr_noexcept { return reinterpret_cast(&const_cast(reinterpret_cast(x))); } + const_pointer address(const_reference x) const mi_attr_noexcept { return reinterpret_cast(&const_cast(reinterpret_cast(x))); } + #endif }; template bool operator==(const mi_stl_allocator& , const mi_stl_allocator& ) mi_attr_noexcept { return true; }