Skip to content

Commit fac33f3

Browse files
committed
Use if constexpr
1 parent 7e418f4 commit fac33f3

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

include/pybind11/detail/common.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@
118118
# endif
119119
#endif
120120

121+
#if defined(__cpp_if_constexpr)
122+
# define PYBIND11_IF_CONSTEXPR constexpr
123+
#else
124+
# define PYBIND11_IF_CONSTEXPR
125+
#endif
126+
121127
#if defined(PYBIND11_CPP20)
122128
# define PYBIND11_CONSTINIT constinit
123129
# define PYBIND11_DTOR_CONSTEXPR constexpr

include/pybind11/pybind11.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ class cpp_function : public function {
204204
auto *rec = unique_rec.get();
205205

206206
/* Store the capture object directly in the function record if there is enough space */
207-
if (sizeof(capture) <= sizeof(rec->data)) {
207+
if PYBIND11_IF_CONSTEXPR (sizeof(capture) <= sizeof(rec->data)) {
208208
/* Without these pragmas, GCC warns that there might not be
209209
enough space to use the placement new operator. However, the
210210
'if' statement above ensures that this is the case. */
@@ -222,7 +222,7 @@ class cpp_function : public function {
222222

223223
// UB without std::launder, but without breaking ABI and/or
224224
// a significant refactoring it's "impossible" to solve.
225-
if (!std::is_trivially_destructible<capture>::value) {
225+
if PYBIND11_IF_CONSTEXPR (!std::is_trivially_destructible<capture>::value) {
226226
rec->free_data = [](function_record *r) {
227227
auto data = PYBIND11_STD_LAUNDER((capture *) &r->data);
228228
(void) data;
@@ -331,7 +331,7 @@ class cpp_function : public function {
331331
using FunctionType = Return (*)(Args...);
332332
constexpr bool is_function_ptr
333333
= std::is_convertible<Func, FunctionType>::value && sizeof(capture) == sizeof(void *);
334-
if (is_function_ptr) {
334+
if PYBIND11_IF_CONSTEXPR (is_function_ptr) {
335335
rec->is_stateless = true;
336336
rec->data[1]
337337
= const_cast<void *>(reinterpret_cast<const void *>(&typeid(FunctionType)));
@@ -1605,7 +1605,7 @@ class class_ : public detail::generic_type {
16051605

16061606
generic_type::initialize(record);
16071607

1608-
if (has_alias) {
1608+
if PYBIND11_IF_CONSTEXPR (has_alias) {
16091609
with_internals([&](internals &internals) {
16101610
auto &instances = record.module_local ? get_local_internals().registered_types_cpp
16111611
: internals.registered_types_cpp;

include/pybind11/pytypes.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1825,7 +1825,7 @@ PYBIND11_NAMESPACE_BEGIN(detail)
18251825
// unsigned type: (A)-1 != (B)-1 when A and B are unsigned types of different sizes).
18261826
template <typename Unsigned>
18271827
Unsigned as_unsigned(PyObject *o) {
1828-
if (sizeof(Unsigned) <= sizeof(unsigned long)) {
1828+
if PYBIND11_IF_CONSTEXPR (sizeof(Unsigned) <= sizeof(unsigned long)) {
18291829
unsigned long v = PyLong_AsUnsignedLong(o);
18301830
return v == (unsigned long) -1 && PyErr_Occurred() ? (Unsigned) -1 : (Unsigned) v;
18311831
}
@@ -1842,7 +1842,7 @@ class int_ : public object {
18421842
template <typename T, detail::enable_if_t<std::is_integral<T>::value, int> = 0>
18431843
// NOLINTNEXTLINE(google-explicit-constructor)
18441844
int_(T value) {
1845-
if (sizeof(T) <= sizeof(long)) {
1845+
if PYBIND11_IF_CONSTEXPR (sizeof(T) <= sizeof(long)) {
18461846
if (std::is_signed<T>::value) {
18471847
m_ptr = PyLong_FromLong((long) value);
18481848
} else {

0 commit comments

Comments
 (0)