Fix Clazy warnings (#4988)

* 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>`

https://github.com/boostorg/unordered/issues/139

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

---------

Co-authored-by: Ralf W. Grosse-Kunstleve <rwgk@google.com>
This commit is contained in:
Christoph Grüninger 2023-12-30 06:59:47 +01:00 committed by GitHub
parent eeac2f4572
commit 976fea05a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 14 deletions

View File

@ -172,7 +172,7 @@ struct list_caster {
auto s = reinterpret_borrow<sequence>(src); auto s = reinterpret_borrow<sequence>(src);
value.clear(); value.clear();
reserve_maybe(s, &value); reserve_maybe(s, &value);
for (auto it : s) { for (const auto &it : s) {
value_conv conv; value_conv conv;
if (!conv.load(it, convert)) { if (!conv.load(it, convert)) {
return false; return false;
@ -247,7 +247,7 @@ public:
return false; return false;
} }
size_t ctr = 0; size_t ctr = 0;
for (auto it : l) { for (const auto &it : l) {
value_conv conv; value_conv conv;
if (!conv.load(it, convert)) { if (!conv.load(it, convert)) {
return false; return false;

View File

@ -103,21 +103,26 @@ private:
int value; 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 // test_unique_nodelete
// Object with a private destructor // Object with a private destructor
class MyObject4;
std::unordered_set<MyObject4 *> myobject4_instances;
class MyObject4 { class MyObject4 {
public: public:
explicit MyObject4(int value) : value{value} { explicit MyObject4(int value) : value{value} {
print_created(this); print_created(this);
myobject4_instances.insert(this); pointer_set<MyObject4>().insert(this);
} }
int value; int value;
static void cleanupAllInstances() { static void cleanupAllInstances() {
auto tmp = std::move(myobject4_instances); auto tmp = std::move(pointer_set<MyObject4>());
myobject4_instances.clear(); pointer_set<MyObject4>().clear();
for (auto *o : tmp) { for (auto *o : tmp) {
delete o; delete o;
} }
@ -125,7 +130,7 @@ public:
private: private:
~MyObject4() { ~MyObject4() {
myobject4_instances.erase(this); pointer_set<MyObject4>().erase(this);
print_destroyed(this); print_destroyed(this);
} }
}; };
@ -133,19 +138,17 @@ private:
// test_unique_deleter // test_unique_deleter
// Object with std::unique_ptr<T, D> where D is not matching the base class // Object with std::unique_ptr<T, D> where D is not matching the base class
// Object with a protected destructor // Object with a protected destructor
class MyObject4a;
std::unordered_set<MyObject4a *> myobject4a_instances;
class MyObject4a { class MyObject4a {
public: public:
explicit MyObject4a(int i) : value{i} { explicit MyObject4a(int i) : value{i} {
print_created(this); print_created(this);
myobject4a_instances.insert(this); pointer_set<MyObject4a>().insert(this);
}; };
int value; int value;
static void cleanupAllInstances() { static void cleanupAllInstances() {
auto tmp = std::move(myobject4a_instances); auto tmp = std::move(pointer_set<MyObject4a>());
myobject4a_instances.clear(); pointer_set<MyObject4a>().clear();
for (auto *o : tmp) { for (auto *o : tmp) {
delete o; delete o;
} }
@ -153,7 +156,7 @@ public:
protected: protected:
virtual ~MyObject4a() { virtual ~MyObject4a() {
myobject4a_instances.erase(this); pointer_set<MyObject4a>().erase(this);
print_destroyed(this); print_destroyed(this);
} }
}; };