From 759221f5c56939f59d8f342a41f8e2d2cacbc8cf Mon Sep 17 00:00:00 2001 From: Jeremy Nimmer Date: Fri, 14 Jun 2019 12:12:51 -0400 Subject: [PATCH] Obey __cpp_sized_deallocation and __cpp_aligned_new Don't assume that just because the language version is C++17 that the standard library offers all C++17 features, too. When using clang-6.0 and --std=c++17 on Ubuntu 18.04 with libstdc++, __cpp_sized_deallocation is false. --- include/pybind11/cast.h | 4 ++-- include/pybind11/pybind11.h | 21 ++++++++++++++------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 605acb366..efc10d65c 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -574,10 +574,10 @@ public: if (type->operator_new) { vptr = type->operator_new(type->type_size); } else { - #if defined(PYBIND11_CPP17) + #ifdef __cpp_aligned_new if (type->type_align > __STDCPP_DEFAULT_NEW_ALIGNMENT__) vptr = ::operator new(type->type_size, - (std::align_val_t) type->type_align); + std::align_val_t(type->type_align)); else #endif vptr = ::operator new(type->type_size); diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index c6237056b..513ceed5d 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -1003,14 +1003,21 @@ void call_operator_delete(T *p, size_t s, size_t) { T::operator delete(p, s); } inline void call_operator_delete(void *p, size_t s, size_t a) { (void)s; (void)a; -#if defined(PYBIND11_CPP17) - if (a > __STDCPP_DEFAULT_NEW_ALIGNMENT__) - ::operator delete(p, s, std::align_val_t(a)); - else + #ifdef __cpp_aligned_new + if (a > __STDCPP_DEFAULT_NEW_ALIGNMENT__) { + #ifdef __cpp_sized_deallocation + ::operator delete(p, s, std::align_val_t(a)); + #else + ::operator delete(p, std::align_val_t(a)); + #endif + return; + } + #endif + #ifdef __cpp_sized_deallocation ::operator delete(p, s); -#else - ::operator delete(p); -#endif + #else + ::operator delete(p); + #endif } NAMESPACE_END(detail)