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>
This commit is contained in:
Michael Goulding 2020-08-14 09:15:50 -07:00 committed by GitHub
parent 2e2de8c87a
commit fb042d692f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 2 additions and 2 deletions

View File

@ -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);
}

View File

@ -169,7 +169,7 @@ extern "C" inline PyObject *pybind11_meta_call(PyObject *type, PyObject *args, P
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);