Skip to content

Commit 3396cce

Browse files
committed
Use if constexpr
1 parent 7e418f4 commit 3396cce

File tree

5 files changed

+26
-15
lines changed

5 files changed

+26
-15
lines changed

include/pybind11/cast.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ struct type_caster<T, enable_if_t<std::is_arithmetic<T>::value && !is_std_char_t
146146
auto index_check = [](PyObject *o) { return hasattr(o, "__index__"); };
147147
#endif
148148

149-
if (std::is_floating_point<T>::value) {
149+
if PYBIND11_IF_CONSTEXPR (std::is_floating_point<T>::value) {
150150
if (convert || PyFloat_Check(src.ptr())) {
151151
py_value = (py_type) PyFloat_AsDouble(src.ptr());
152152
} else {
@@ -171,7 +171,7 @@ struct type_caster<T, enable_if_t<std::is_arithmetic<T>::value && !is_std_char_t
171171
}
172172
}
173173
#endif
174-
if (std::is_unsigned<py_type>::value) {
174+
if PYBIND11_IF_CONSTEXPR (std::is_unsigned<py_type>::value) {
175175
py_value = as_unsigned<py_type>(src_or_index.ptr());
176176
} else { // signed integer:
177177
py_value = sizeof(T) <= sizeof(long)
@@ -559,7 +559,7 @@ struct type_caster<CharT, enable_if_t<is_std_char_type<CharT>::value>> {
559559
}
560560

561561
static handle cast(CharT src, return_value_policy policy, handle parent) {
562-
if (std::is_same<char, CharT>::value) {
562+
if PYBIND11_IF_CONSTEXPR (std::is_same<char, CharT>::value) {
563563
handle s = PyUnicode_DecodeLatin1((const char *) &src, 1, nullptr);
564564
if (!s) {
565565
throw error_already_set();

include/pybind11/detail/common.h

+8
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,14 @@
118118
# endif
119119
#endif
120120

121+
#if defined(__cpp_if_constexpr)
122+
# define PYBIND11_HAS_IF_CONSTEXPR 1
123+
# define PYBIND11_IF_CONSTEXPR constexpr
124+
#else
125+
# define PYBIND11_HAS_IF_CONSTEXPR 0
126+
# define PYBIND11_IF_CONSTEXPR
127+
#endif
128+
121129
#if defined(PYBIND11_CPP20)
122130
# define PYBIND11_CONSTINIT constinit
123131
# define PYBIND11_DTOR_CONSTEXPR constexpr

include/pybind11/pybind11.h

+4-4
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

+7-4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
3535

3636
PYBIND11_WARNING_DISABLE_MSVC(4127)
37+
#if PYBIND11_HAS_IF_CONSTEXPR
38+
PYBIND11_WARNING_DISABLE_MSVC(4702)
39+
#endif
3740

3841
/* A few forward declarations */
3942
class handle;
@@ -1825,7 +1828,7 @@ PYBIND11_NAMESPACE_BEGIN(detail)
18251828
// unsigned type: (A)-1 != (B)-1 when A and B are unsigned types of different sizes).
18261829
template <typename Unsigned>
18271830
Unsigned as_unsigned(PyObject *o) {
1828-
if (sizeof(Unsigned) <= sizeof(unsigned long)) {
1831+
if PYBIND11_IF_CONSTEXPR (sizeof(Unsigned) <= sizeof(unsigned long)) {
18291832
unsigned long v = PyLong_AsUnsignedLong(o);
18301833
return v == (unsigned long) -1 && PyErr_Occurred() ? (Unsigned) -1 : (Unsigned) v;
18311834
}
@@ -1842,14 +1845,14 @@ class int_ : public object {
18421845
template <typename T, detail::enable_if_t<std::is_integral<T>::value, int> = 0>
18431846
// NOLINTNEXTLINE(google-explicit-constructor)
18441847
int_(T value) {
1845-
if (sizeof(T) <= sizeof(long)) {
1846-
if (std::is_signed<T>::value) {
1848+
if PYBIND11_IF_CONSTEXPR (sizeof(T) <= sizeof(long)) {
1849+
if PYBIND11_IF_CONSTEXPR (std::is_signed<T>::value) {
18471850
m_ptr = PyLong_FromLong((long) value);
18481851
} else {
18491852
m_ptr = PyLong_FromUnsignedLong((unsigned long) value);
18501853
}
18511854
} else {
1852-
if (std::is_signed<T>::value) {
1855+
if PYBIND11_IF_CONSTEXPR (std::is_signed<T>::value) {
18531856
m_ptr = PyLong_FromLongLong((long long) value);
18541857
} else {
18551858
m_ptr = PyLong_FromUnsignedLongLong((unsigned long long) value);

include/pybind11/stl.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ struct set_caster {
189189

190190
template <typename T>
191191
static handle cast(T &&src, return_value_policy policy, handle parent) {
192-
if (!std::is_lvalue_reference<T>::value) {
192+
if PYBIND11_IF_CONSTEXPR (!std::is_lvalue_reference<T>::value) {
193193
policy = return_value_policy_override<Key>::policy(policy);
194194
}
195195
pybind11::set s;
@@ -256,7 +256,7 @@ struct map_caster {
256256
dict d;
257257
return_value_policy policy_key = policy;
258258
return_value_policy policy_value = policy;
259-
if (!std::is_lvalue_reference<T>::value) {
259+
if PYBIND11_IF_CONSTEXPR (!std::is_lvalue_reference<T>::value) {
260260
policy_key = return_value_policy_override<Key>::policy(policy_key);
261261
policy_value = return_value_policy_override<Value>::policy(policy_value);
262262
}
@@ -324,7 +324,7 @@ struct list_caster {
324324
public:
325325
template <typename T>
326326
static handle cast(T &&src, return_value_policy policy, handle parent) {
327-
if (!std::is_lvalue_reference<T>::value) {
327+
if PYBIND11_IF_CONSTEXPR (!std::is_lvalue_reference<T>::value) {
328328
policy = return_value_policy_override<Value>::policy(policy);
329329
}
330330
list l(src.size());
@@ -513,7 +513,7 @@ struct optional_caster {
513513
if (!src) {
514514
return none().release();
515515
}
516-
if (!std::is_lvalue_reference<T>::value) {
516+
if PYBIND11_IF_CONSTEXPR (!std::is_lvalue_reference<T>::value) {
517517
policy = return_value_policy_override<Value>::policy(policy);
518518
}
519519
// NOLINTNEXTLINE(bugprone-unchecked-optional-access)

0 commit comments

Comments
 (0)