Skip to content

Commit

Permalink
Fix Clazy warnings (#4988)
Browse files Browse the repository at this point in the history
* stl.h: Use C++11 range-loops with const reference

This saves copy-ctor and dtor for non-trivial types by value
Found by clazy (range-loop-reference)

* test_smart_ptr.cpp cleanup

Introduce `pointer_set<T>`

boostorg/unordered#139

> Based on the standard, the first move should leave a in a "valid but unspecified state";

---------

Co-authored-by: Ralf W. Grosse-Kunstleve <[email protected]>
  • Loading branch information
gruenich and Ralf W. Grosse-Kunstleve authored Dec 30, 2023
1 parent eeac2f4 commit 976fea0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
4 changes: 2 additions & 2 deletions include/pybind11/stl.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ struct list_caster {
auto s = reinterpret_borrow<sequence>(src);
value.clear();
reserve_maybe(s, &value);
for (auto it : s) {
for (const auto &it : s) {
value_conv conv;
if (!conv.load(it, convert)) {
return false;
Expand Down Expand Up @@ -247,7 +247,7 @@ struct array_caster {
return false;
}
size_t ctr = 0;
for (auto it : l) {
for (const auto &it : l) {
value_conv conv;
if (!conv.load(it, convert)) {
return false;
Expand Down
27 changes: 15 additions & 12 deletions tests/test_smart_ptr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,57 +103,60 @@ class MyObject3 : public std::enable_shared_from_this<MyObject3> {
int value;
};

template <typename T>
std::unordered_set<T *> &pointer_set() {
// https://google.github.io/styleguide/cppguide.html#Static_and_Global_Variables
static auto singleton = new std::unordered_set<T *>();
return *singleton;
}

// test_unique_nodelete
// Object with a private destructor
class MyObject4;
std::unordered_set<MyObject4 *> myobject4_instances;
class MyObject4 {
public:
explicit MyObject4(int value) : value{value} {
print_created(this);
myobject4_instances.insert(this);
pointer_set<MyObject4>().insert(this);
}
int value;

static void cleanupAllInstances() {
auto tmp = std::move(myobject4_instances);
myobject4_instances.clear();
auto tmp = std::move(pointer_set<MyObject4>());
pointer_set<MyObject4>().clear();
for (auto *o : tmp) {
delete o;
}
}

private:
~MyObject4() {
myobject4_instances.erase(this);
pointer_set<MyObject4>().erase(this);
print_destroyed(this);
}
};

// test_unique_deleter
// Object with std::unique_ptr<T, D> where D is not matching the base class
// Object with a protected destructor
class MyObject4a;
std::unordered_set<MyObject4a *> myobject4a_instances;
class MyObject4a {
public:
explicit MyObject4a(int i) : value{i} {
print_created(this);
myobject4a_instances.insert(this);
pointer_set<MyObject4a>().insert(this);
};
int value;

static void cleanupAllInstances() {
auto tmp = std::move(myobject4a_instances);
myobject4a_instances.clear();
auto tmp = std::move(pointer_set<MyObject4a>());
pointer_set<MyObject4a>().clear();
for (auto *o : tmp) {
delete o;
}
}

protected:
virtual ~MyObject4a() {
myobject4a_instances.erase(this);
pointer_set<MyObject4a>().erase(this);
print_destroyed(this);
}
};
Expand Down

0 comments on commit 976fea0

Please sign in to comment.