Skip to content

Commit 759221f

Browse files
jwnimmer-triwjakob
authored andcommitted
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.
1 parent 6c29cbf commit 759221f

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

include/pybind11/cast.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,10 +574,10 @@ class type_caster_generic {
574574
if (type->operator_new) {
575575
vptr = type->operator_new(type->type_size);
576576
} else {
577-
#if defined(PYBIND11_CPP17)
577+
#ifdef __cpp_aligned_new
578578
if (type->type_align > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
579579
vptr = ::operator new(type->type_size,
580-
(std::align_val_t) type->type_align);
580+
std::align_val_t(type->type_align));
581581
else
582582
#endif
583583
vptr = ::operator new(type->type_size);

include/pybind11/pybind11.h

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,14 +1003,21 @@ void call_operator_delete(T *p, size_t s, size_t) { T::operator delete(p, s); }
10031003

10041004
inline void call_operator_delete(void *p, size_t s, size_t a) {
10051005
(void)s; (void)a;
1006-
#if defined(PYBIND11_CPP17)
1007-
if (a > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
1008-
::operator delete(p, s, std::align_val_t(a));
1009-
else
1006+
#ifdef __cpp_aligned_new
1007+
if (a > __STDCPP_DEFAULT_NEW_ALIGNMENT__) {
1008+
#ifdef __cpp_sized_deallocation
1009+
::operator delete(p, s, std::align_val_t(a));
1010+
#else
1011+
::operator delete(p, std::align_val_t(a));
1012+
#endif
1013+
return;
1014+
}
1015+
#endif
1016+
#ifdef __cpp_sized_deallocation
10101017
::operator delete(p, s);
1011-
#else
1012-
::operator delete(p);
1013-
#endif
1018+
#else
1019+
::operator delete(p);
1020+
#endif
10141021
}
10151022

10161023
NAMESPACE_END(detail)

0 commit comments

Comments
 (0)