From c3d926ac1d8af61e24c48182159d6ccd3c519ef4 Mon Sep 17 00:00:00 2001 From: cyy Date: Sun, 29 Sep 2024 19:20:07 +0800 Subject: [PATCH] Modernize c++ code --- tests/test_class.cpp | 5 +++-- tests/test_exceptions.cpp | 8 ++++---- tests/test_kwargs_and_defaults.cpp | 2 +- tests/test_pickling.cpp | 4 ++-- tests/test_tagbased_polymorphic.cpp | 4 +++- tests/test_virtual_functions.cpp | 3 ++- 6 files changed, 15 insertions(+), 11 deletions(-) diff --git a/tests/test_class.cpp b/tests/test_class.cpp index 9001d86b19..ef0c07a0e8 100644 --- a/tests/test_class.cpp +++ b/tests/test_class.cpp @@ -20,6 +20,7 @@ #include "local_bindings.h" #include "pybind11_tests.h" +#include #include PYBIND11_WARNING_DISABLE_MSVC(4324) @@ -92,8 +93,8 @@ TEST_SUBMODULE(class_, m) { // test_inheritance class Pet { public: - Pet(const std::string &name, const std::string &species) - : m_name(name), m_species(species) {} + Pet(std::string name, std::string species) + : m_name(std::move(name)), m_species(std::move(species)) {} std::string name() const { return m_name; } std::string species() const { return m_species; } diff --git a/tests/test_exceptions.cpp b/tests/test_exceptions.cpp index 0a970065b4..5c4f16ed01 100644 --- a/tests/test_exceptions.cpp +++ b/tests/test_exceptions.cpp @@ -90,14 +90,14 @@ class MyException6 : public std::exception { }; struct PythonCallInDestructor { - explicit PythonCallInDestructor(const py::dict &d) : d(d) {} + explicit PythonCallInDestructor(py::dict d) : d(std::move(d)) {} ~PythonCallInDestructor() { d["good"] = true; } py::dict d; }; struct PythonAlreadySetInDestructor { - explicit PythonAlreadySetInDestructor(const py::str &s) : s(s) {} + explicit PythonAlreadySetInDestructor(py::str s) : s(std::move(s)) {} ~PythonAlreadySetInDestructor() { py::dict foo; try { @@ -112,12 +112,12 @@ struct PythonAlreadySetInDestructor { }; struct CustomData { - explicit CustomData(const std::string &a) : a(a) {} + explicit CustomData(std::string a) : a(std::move(a)) {} std::string a; }; struct MyException7 { - explicit MyException7(const CustomData &message) : message(message) {} + explicit MyException7(CustomData message) : message(std::move(message)) {} CustomData message; }; diff --git a/tests/test_kwargs_and_defaults.cpp b/tests/test_kwargs_and_defaults.cpp index 09036ccd51..88626f4508 100644 --- a/tests/test_kwargs_and_defaults.cpp +++ b/tests/test_kwargs_and_defaults.cpp @@ -65,7 +65,7 @@ TEST_SUBMODULE(kwargs_and_defaults, m) { struct CustomRepr { std::string repr_string; - explicit CustomRepr(const std::string &repr) : repr_string(repr) {} + explicit CustomRepr(std::string repr) : repr_string(std::move(repr)) {} std::string __repr__() const { return repr_string; } }; diff --git a/tests/test_pickling.cpp b/tests/test_pickling.cpp index e154bc483c..3e9d36446d 100644 --- a/tests/test_pickling.cpp +++ b/tests/test_pickling.cpp @@ -66,7 +66,7 @@ TEST_SUBMODULE(pickling, m) { // test_roundtrip class Pickleable { public: - explicit Pickleable(const std::string &value) : m_value(value) {} + explicit Pickleable(std::string value) : m_value(std::move(value)) {} const std::string &value() const { return m_value; } void setExtra1(int extra1) { m_extra1 = extra1; } @@ -133,7 +133,7 @@ TEST_SUBMODULE(pickling, m) { // test_roundtrip_with_dict class PickleableWithDict { public: - explicit PickleableWithDict(const std::string &value) : value(value) {} + explicit PickleableWithDict(std::string value) : value(std::move(value)) {} std::string value; int extra; diff --git a/tests/test_tagbased_polymorphic.cpp b/tests/test_tagbased_polymorphic.cpp index 13e5ed3198..7c83550c69 100644 --- a/tests/test_tagbased_polymorphic.cpp +++ b/tests/test_tagbased_polymorphic.cpp @@ -11,6 +11,8 @@ #include "pybind11_tests.h" +#include + struct Animal { // Make this type also a "standard" polymorphic type, to confirm that // specializing polymorphic_type_hook using enable_if_t still works @@ -35,7 +37,7 @@ struct Animal { const std::string name; protected: - Animal(const std::string &_name, Kind _kind) : kind(_kind), name(_name) {} + Animal(std::string _name, Kind _kind) : kind(_kind), name(std::move(_name)) {} }; struct Dog : Animal { diff --git a/tests/test_virtual_functions.cpp b/tests/test_virtual_functions.cpp index a6164eb81d..f827710221 100644 --- a/tests/test_virtual_functions.cpp +++ b/tests/test_virtual_functions.cpp @@ -13,6 +13,7 @@ #include "pybind11_tests.h" #include +#include /* This is an example class that we'll want to be able to extend from Python */ class ExampleVirt { @@ -357,7 +358,7 @@ TEST_SUBMODULE(virtual_functions, m) { }; std::string v; A a; - explicit OverrideTest(const std::string &v) : v{v} {} + explicit OverrideTest(std::string v) : v{std::move(v)} {} OverrideTest() = default; OverrideTest(const OverrideTest &) = delete; virtual std::string str_value() { return v; }