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 <Michael.Goulding@microsoft.com>
Co-authored-by: Yannick Jadoul <yannick.jadoul@belgacom.net>
diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h
index c1d0bbc..4901ea3 100644
--- a/include/pybind11/cast.h
+++ b/include/pybind11/cast.h
@@ -458,7 +458,7 @@
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 d58f04e..8d36744 100644
--- a/include/pybind11/detail/class.h
+++ b/include/pybind11/detail/class.h
@@ -169,7 +169,7 @@
auto instance = reinterpret_cast<detail::instance *>(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);