From fb042d692ffbbc9ec39546c8e734596ddaf93b7e Mon Sep 17 00:00:00 2001 From: Michael Goulding Date: Fri, 14 Aug 2020 09:15:50 -0700 Subject: [PATCH] Fix warning C26817 on copying in `for (auto vh : value_and_holder(...))` (#2382) * Fix warning C26817: Potentially expensive copy of variable 'vh' in range-for loop. Consider making it a const reference (es.71). * Replace another instance of `for (auto vh : values_and_holders(...))` with `auto vh &` (found by @bstaletic) Co-authored-by: Michael Goulding Co-authored-by: Yannick Jadoul --- include/pybind11/cast.h | 2 +- include/pybind11/detail/class.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index c1d0bbc34..4901ea391 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -458,7 +458,7 @@ PYBIND11_NOINLINE inline handle get_object_handle(const void *ptr, const detail: auto &instances = get_internals().registered_instances; auto range = instances.equal_range(ptr); for (auto it = range.first; it != range.second; ++it) { - for (auto vh : values_and_holders(it->second)) { + for (const auto &vh : values_and_holders(it->second)) { if (vh.type == type) return handle((PyObject *) it->second); } diff --git a/include/pybind11/detail/class.h b/include/pybind11/detail/class.h index d58f04e61..8d36744f2 100644 --- a/include/pybind11/detail/class.h +++ b/include/pybind11/detail/class.h @@ -169,7 +169,7 @@ extern "C" inline PyObject *pybind11_meta_call(PyObject *type, PyObject *args, P auto instance = reinterpret_cast(self); // Ensure that the base __init__ function(s) were called - for (auto vh : values_and_holders(instance)) { + for (const auto &vh : values_and_holders(instance)) { if (!vh.holder_constructed()) { PyErr_Format(PyExc_TypeError, "%.200s.__init__() must be called when overriding __init__", vh.type->type->tp_name);