Skip to content

Commit c01273e

Browse files
committed
Use if constexpr
1 parent f260693 commit c01273e

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
@@ -222,7 +222,7 @@ class cpp_function : public function {
222222
auto *rec = unique_rec.get();
223223

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

241241
// UB without std::launder, but without breaking ABI and/or
242242
// a significant refactoring it's "impossible" to solve.
243-
if (!std::is_trivially_destructible<capture>::value) {
243+
if PYBIND11_IF_CONSTEXPR (!std::is_trivially_destructible<capture>::value) {
244244
rec->free_data = [](function_record *r) {
245245
auto data = PYBIND11_STD_LAUNDER((capture *) &r->data);
246246
(void) data;
@@ -349,7 +349,7 @@ class cpp_function : public function {
349349
using FunctionType = Return (*)(Args...);
350350
constexpr bool is_function_ptr
351351
= std::is_convertible<Func, FunctionType>::value && sizeof(capture) == sizeof(void *);
352-
if (is_function_ptr) {
352+
if PYBIND11_IF_CONSTEXPR (is_function_ptr) {
353353
rec->is_stateless = true;
354354
rec->data[1]
355355
= const_cast<void *>(reinterpret_cast<const void *>(&typeid(FunctionType)));
@@ -1595,7 +1595,7 @@ class class_ : public detail::generic_type {
15951595

15961596
generic_type::initialize(record);
15971597

1598-
if (has_alias) {
1598+
if PYBIND11_IF_CONSTEXPR (has_alias) {
15991599
auto &instances = record.module_local ? get_local_internals().registered_types_cpp
16001600
: get_internals().registered_types_cpp;
16011601
instances[std::type_index(typeid(type_alias))]

include/pybind11/pytypes.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1797,7 +1797,7 @@ PYBIND11_NAMESPACE_BEGIN(detail)
17971797
// unsigned type: (A)-1 != (B)-1 when A and B are unsigned types of different sizes).
17981798
template <typename Unsigned>
17991799
Unsigned as_unsigned(PyObject *o) {
1800-
if (sizeof(Unsigned) <= sizeof(unsigned long)) {
1800+
if PYBIND11_IF_CONSTEXPR (sizeof(Unsigned) <= sizeof(unsigned long)) {
18011801
unsigned long v = PyLong_AsUnsignedLong(o);
18021802
return v == (unsigned long) -1 && PyErr_Occurred() ? (Unsigned) -1 : (Unsigned) v;
18031803
}
@@ -1814,7 +1814,7 @@ class int_ : public object {
18141814
template <typename T, detail::enable_if_t<std::is_integral<T>::value, int> = 0>
18151815
// NOLINTNEXTLINE(google-explicit-constructor)
18161816
int_(T value) {
1817-
if (sizeof(T) <= sizeof(long)) {
1817+
if PYBIND11_IF_CONSTEXPR (sizeof(T) <= sizeof(long)) {
18181818
if (std::is_signed<T>::value) {
18191819
m_ptr = PyLong_FromLong((long) value);
18201820
} else {

0 commit comments

Comments
 (0)