Skip to content

Commit 7d7734f

Browse files
committed
Adding PYBIND11_COMPAT_BOOL_CAST to appease MSVC 2015 warning C4800.
1 parent ff97f10 commit 7d7734f

File tree

6 files changed

+13
-7
lines changed

6 files changed

+13
-7
lines changed

include/pybind11/buffer_info.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ struct buffer_info {
8383
view->strides
8484
? std::vector<ssize_t>(view->strides, view->strides + view->ndim)
8585
: detail::c_strides({view->shape, view->shape + view->ndim}, view->itemsize),
86-
view->readonly) {
86+
PYBIND11_COMPAT_BOOL_CAST(view->readonly)) {
8787
this->m_view = view;
8888
this->ownview = ownview;
8989
}

include/pybind11/cast.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ template <> class type_caster<bool> {
322322
}
323323
#endif
324324
if (res == 0 || res == 1) {
325-
value = (bool) res;
325+
value = PYBIND11_COMPAT_BOOL_CAST(res);
326326
return true;
327327
}
328328
PyErr_Clear();

include/pybind11/detail/common.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,10 @@ extern "C" {
265265
#define PYBIND11_ENSURE_INTERNALS_READY \
266266
pybind11::detail::get_internals();
267267

268+
// Paying tribute to MSVC 2015 warning
269+
// C4800: 'int': forcing value to bool 'true' or 'false' (performance warning)
270+
#define PYBIND11_COMPAT_BOOL_CAST(...) ((__VA_ARGS__) ? true : false)
271+
268272
#define PYBIND11_CHECK_PYTHON_VERSION \
269273
{ \
270274
const char *compiled_ver = PYBIND11_TOSTRING(PY_MAJOR_VERSION) \

include/pybind11/detail/type_caster_base.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ struct value_and_holder {
231231
return reinterpret_cast<V *&>(vh[0]);
232232
}
233233
// True if this `value_and_holder` has a non-null value pointer
234-
explicit operator bool() const { return value_ptr(); }
234+
explicit operator bool() const { return value_ptr() != nullptr; }
235235

236236
template <typename H> H &holder() const {
237237
return reinterpret_cast<H &>(vh[1]);
@@ -252,7 +252,8 @@ struct value_and_holder {
252252
bool instance_registered() const {
253253
return inst->simple_layout
254254
? inst->simple_instance_registered
255-
: inst->nonsimple.status[index] & instance::status_instance_registered;
255+
: PYBIND11_COMPAT_BOOL_CAST(
256+
inst->nonsimple.status[index] & instance::status_instance_registered);
256257
}
257258
void set_instance_registered(bool v = true) const {
258259
if (inst->simple_layout)

include/pybind11/pybind11.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
# pragma warning(push)
1919
# pragma warning(disable: 4100) // warning C4100: Unreferenced formal parameter
2020
# pragma warning(disable: 4127) // warning C4127: Conditional expression is constant
21-
# pragma warning(disable: 4800) // warning C4800: 'int': forcing value to bool 'true' or 'false' (performance warning)
2221
# pragma warning(disable: 4522) // warning C4522: multiple assignment operators specified
2322
# pragma warning(disable: 4505) // warning C4505: 'PySlice_GetIndicesEx': unreferenced local function has been removed (PyPy only)
2423
#elif defined(__GNUG__) && !defined(__clang__)

include/pybind11/pytypes.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,9 @@ class PYBIND11_EXPORT error_already_set : public std::runtime_error {
367367
/// Check if the currently trapped error type matches the given Python exception class (or a
368368
/// subclass thereof). May also be passed a tuple to search for any exception class matches in
369369
/// the given tuple.
370-
bool matches(handle exc) const { return PyErr_GivenExceptionMatches(m_type.ptr(), exc.ptr()); }
370+
bool matches(handle exc) const {
371+
return PYBIND11_COMPAT_BOOL_CAST(PyErr_GivenExceptionMatches(m_type.ptr(), exc.ptr()));
372+
}
371373

372374
const object& type() const { return m_type; }
373375
const object& value() const { return m_value; }
@@ -846,7 +848,7 @@ PYBIND11_NAMESPACE_END(detail)
846848
Name(handle h, borrowed_t) : Parent(h, borrowed_t{}) { } \
847849
Name(handle h, stolen_t) : Parent(h, stolen_t{}) { } \
848850
PYBIND11_DEPRECATED("Use py::isinstance<py::python_type>(obj) instead") \
849-
bool check() const { return m_ptr != nullptr && (bool) CheckFun(m_ptr); } \
851+
bool check() const { return m_ptr != nullptr && PYBIND11_COMPAT_BOOL_CAST(CheckFun(m_ptr)); } \
850852
static bool check_(handle h) { return h.ptr() != nullptr && CheckFun(h.ptr()); } \
851853
template <typename Policy_> \
852854
Name(const ::pybind11::detail::accessor<Policy_> &a) : Name(object(a)) { }

0 commit comments

Comments
 (0)