Skip to content

Commit c11f980

Browse files
committed
Modernize code
1 parent 7e418f4 commit c11f980

7 files changed

+19
-16
lines changed

tests/test_class.cpp

+5-6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "local_bindings.h"
2121
#include "pybind11_tests.h"
2222

23+
#include <memory>
2324
#include <utility>
2425

2526
PYBIND11_WARNING_DISABLE_MSVC(4324)
@@ -92,8 +93,8 @@ TEST_SUBMODULE(class_, m) {
9293
// test_inheritance
9394
class Pet {
9495
public:
95-
Pet(const std::string &name, const std::string &species)
96-
: m_name(name), m_species(species) {}
96+
Pet(std::string name, std::string species)
97+
: m_name(std::move(name)), m_species(std::move(species)) {}
9798
std::string name() const { return m_name; }
9899
std::string species() const { return m_species; }
99100

@@ -223,13 +224,11 @@ TEST_SUBMODULE(class_, m) {
223224
// test_override_static
224225
// #511: problem with inheritance + overwritten def_static
225226
struct MyBase {
226-
static std::unique_ptr<MyBase> make() { return std::unique_ptr<MyBase>(new MyBase()); }
227+
static std::unique_ptr<MyBase> make() { return std::make_unique<MyBase>(); }
227228
};
228229

229230
struct MyDerived : MyBase {
230-
static std::unique_ptr<MyDerived> make() {
231-
return std::unique_ptr<MyDerived>(new MyDerived());
232-
}
231+
static std::unique_ptr<MyDerived> make() { return std::make_unique<MyDerived>(); }
233232
};
234233

235234
py::class_<MyBase>(m, "MyBase").def_static("make", &MyBase::make);

tests/test_copy_move.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "constructor_stats.h"
1414
#include "pybind11_tests.h"
1515

16+
#include <memory>
1617
#include <type_traits>
1718

1819
template <typename derived>
@@ -294,7 +295,7 @@ TEST_SUBMODULE(copy_move_policies, m) {
294295
// `py::return_value_policy::move`
295296
m.def(
296297
"get_moveissue1",
297-
[](int i) { return std::unique_ptr<MoveIssue1>(new MoveIssue1(i)); },
298+
[](int i) { return std::make_unique<MoveIssue1>(i); },
298299
py::return_value_policy::move);
299300
m.def("get_moveissue2", [](int i) { return MoveIssue2(i); }, py::return_value_policy::move);
300301

tests/test_exceptions.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,14 @@ class MyException6 : public std::exception {
9090
};
9191

9292
struct PythonCallInDestructor {
93-
explicit PythonCallInDestructor(const py::dict &d) : d(d) {}
93+
explicit PythonCallInDestructor(py::dict d) : d(std::move(d)) {}
9494
~PythonCallInDestructor() { d["good"] = true; }
9595

9696
py::dict d;
9797
};
9898

9999
struct PythonAlreadySetInDestructor {
100-
explicit PythonAlreadySetInDestructor(const py::str &s) : s(s) {}
100+
explicit PythonAlreadySetInDestructor(py::str s) : s(std::move(s)) {}
101101
~PythonAlreadySetInDestructor() {
102102
py::dict foo;
103103
try {
@@ -112,12 +112,12 @@ struct PythonAlreadySetInDestructor {
112112
};
113113

114114
struct CustomData {
115-
explicit CustomData(const std::string &a) : a(a) {}
115+
explicit CustomData(std::string a) : a(std::move(a)) {}
116116
std::string a;
117117
};
118118

119119
struct MyException7 {
120-
explicit MyException7(const CustomData &message) : message(message) {}
120+
explicit MyException7(CustomData message) : message(std::move(message)) {}
121121
CustomData message;
122122
};
123123

tests/test_kwargs_and_defaults.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ TEST_SUBMODULE(kwargs_and_defaults, m) {
6565
struct CustomRepr {
6666
std::string repr_string;
6767

68-
explicit CustomRepr(const std::string &repr) : repr_string(repr) {}
68+
explicit CustomRepr(std::string repr) : repr_string(std::move(repr)) {}
6969

7070
std::string __repr__() const { return repr_string; }
7171
};

tests/test_pickling.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ TEST_SUBMODULE(pickling, m) {
6666
// test_roundtrip
6767
class Pickleable {
6868
public:
69-
explicit Pickleable(const std::string &value) : m_value(value) {}
69+
explicit Pickleable(std::string value) : m_value(std::move(value)) {}
7070
const std::string &value() const { return m_value; }
7171

7272
void setExtra1(int extra1) { m_extra1 = extra1; }
@@ -133,7 +133,7 @@ TEST_SUBMODULE(pickling, m) {
133133
// test_roundtrip_with_dict
134134
class PickleableWithDict {
135135
public:
136-
explicit PickleableWithDict(const std::string &value) : value(value) {}
136+
explicit PickleableWithDict(std::string value) : value(std::move(value)) {}
137137

138138
std::string value;
139139
int extra;

tests/test_tagbased_polymorphic.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
#include "pybind11_tests.h"
1313

14+
#include <utility>
15+
1416
struct Animal {
1517
// Make this type also a "standard" polymorphic type, to confirm that
1618
// specializing polymorphic_type_hook using enable_if_t still works
@@ -35,7 +37,7 @@ struct Animal {
3537
const std::string name;
3638

3739
protected:
38-
Animal(const std::string &_name, Kind _kind) : kind(_kind), name(_name) {}
40+
Animal(std::string _name, Kind _kind) : kind(_kind), name(std::move(_name)) {}
3941
};
4042

4143
struct Dog : Animal {

tests/test_virtual_functions.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "pybind11_tests.h"
1414

1515
#include <thread>
16+
#include <utility>
1617

1718
/* This is an example class that we'll want to be able to extend from Python */
1819
class ExampleVirt {
@@ -357,7 +358,7 @@ TEST_SUBMODULE(virtual_functions, m) {
357358
};
358359
std::string v;
359360
A a;
360-
explicit OverrideTest(const std::string &v) : v{v} {}
361+
explicit OverrideTest(std::string v) : v{std::move(v)} {}
361362
OverrideTest() = default;
362363
OverrideTest(const OverrideTest &) = delete;
363364
virtual std::string str_value() { return v; }

0 commit comments

Comments
 (0)