Unverified Commit 6abf2baa authored by Ralf W. Grosse-Kunstleve's avatar Ralf W. Grosse-Kunstleve Committed by GitHub
Browse files

CodeHealth: Enabling clang-tidy google-explicit-constructor (#3250)

* Adding google-explicit-constructor to .clang-tidy

* clang-tidy explicit attr.h (all automatic)

* clang-tidy explicit cast.h (all automatic)

* clang-tidy detail/init.h (1 NOLINT)

* clang-tidy detail/type_caster_base.h (2 NOLINT)

* clang-tidy pybind11.h (7 NOLINT)

* clang-tidy detail/common.h (3 NOLINT)

* clang-tidy detail/descr.h (2 NOLINT)

* clang-tidy pytypes.h (23 NOLINT, only 1 explicit)

* clang-tidy eigen.h (7 NOLINT, 0 explicit)

* Adding 2 explicit in functional.h

* Adding 4 explicit in iostream.h

* clang-tidy numpy.h (1 NOLINT, 1 explicit)

* clang-tidy embed.h (0 NOLINT, 1 explicit)

* clang-tidy tests/local_bindings.h (0 NOLINT, 4 explicit)

* clang-tidy tests/pybind11_cross_module_tests.cpp (0 NOLINT, 1 explicit)

* clang-tidy tests/pybind11_tests.h (0 NOLINT, 2 explicit)

* clang-tidy tests/test_buffers.cpp (0 NOLINT, 2 explicit)

* clang-tidy tests/test_builtin_casters.cpp (0 NOLINT, 4 explicit)

* clang-tidy tests/test_class.cpp (0 NOLINT, 6 explicit)

* clang-tidy tests/test_copy_move.cpp (0 NOLINT, 7 explicit)

* clang-tidy tests/test_embed/external_module.cpp (0 NOLINT, 1 explicit)

* clang-tidy tests/test_embed/test_interpreter.cpp (0 NOLINT, 1 explicit)

* clang-tidy tests/object.h (0 NOLINT, 2 explicit)

* clang-tidy batch of fully automatic fixes.

* Workaround for MSVC 19.16.27045.0 C++17 Python 2 C++ syntax error.
parent 39a0aac8
...@@ -27,7 +27,7 @@ ...@@ -27,7 +27,7 @@
// test_brace_initialization // test_brace_initialization
struct NoBraceInitialization { struct NoBraceInitialization {
NoBraceInitialization(std::vector<int> v) : vec{std::move(v)} {} explicit NoBraceInitialization(std::vector<int> v) : vec{std::move(v)} {}
template <typename T> template <typename T>
NoBraceInitialization(std::initializer_list<T> l) : vec(l) {} NoBraceInitialization(std::initializer_list<T> l) : vec(l) {}
...@@ -65,18 +65,18 @@ TEST_SUBMODULE(class_, m) { ...@@ -65,18 +65,18 @@ TEST_SUBMODULE(class_, m) {
class Dog : public Pet { class Dog : public Pet {
public: public:
Dog(const std::string &name) : Pet(name, "dog") {} explicit Dog(const std::string &name) : Pet(name, "dog") {}
std::string bark() const { return "Woof!"; } std::string bark() const { return "Woof!"; }
}; };
class Rabbit : public Pet { class Rabbit : public Pet {
public: public:
Rabbit(const std::string &name) : Pet(name, "parrot") {} explicit Rabbit(const std::string &name) : Pet(name, "parrot") {}
}; };
class Hamster : public Pet { class Hamster : public Pet {
public: public:
Hamster(const std::string &name) : Pet(name, "rodent") {} explicit Hamster(const std::string &name) : Pet(name, "rodent") {}
}; };
class Chimera : public Pet { class Chimera : public Pet {
...@@ -208,7 +208,7 @@ TEST_SUBMODULE(class_, m) { ...@@ -208,7 +208,7 @@ TEST_SUBMODULE(class_, m) {
struct ConvertibleFromUserType { struct ConvertibleFromUserType {
int i; int i;
ConvertibleFromUserType(UserType u) : i(u.value()) { } explicit ConvertibleFromUserType(UserType u) : i(u.value()) {}
}; };
py::class_<ConvertibleFromUserType>(m, "AcceptsUserType") py::class_<ConvertibleFromUserType>(m, "AcceptsUserType")
...@@ -263,7 +263,7 @@ TEST_SUBMODULE(class_, m) { ...@@ -263,7 +263,7 @@ TEST_SUBMODULE(class_, m) {
}; };
struct PyAliasedHasOpNewDelSize : AliasedHasOpNewDelSize { struct PyAliasedHasOpNewDelSize : AliasedHasOpNewDelSize {
PyAliasedHasOpNewDelSize() = default; PyAliasedHasOpNewDelSize() = default;
PyAliasedHasOpNewDelSize(int) { } explicit PyAliasedHasOpNewDelSize(int) {}
std::uint64_t j; std::uint64_t j;
}; };
struct HasOpNewDelBoth { struct HasOpNewDelBoth {
......
...@@ -37,7 +37,7 @@ template <> lacking_move_ctor empty<lacking_move_ctor>::instance_ = {}; ...@@ -37,7 +37,7 @@ template <> lacking_move_ctor empty<lacking_move_ctor>::instance_ = {};
class MoveOnlyInt { class MoveOnlyInt {
public: public:
MoveOnlyInt() { print_default_created(this); } MoveOnlyInt() { print_default_created(this); }
MoveOnlyInt(int v) : value{v} { print_created(this, value); } explicit MoveOnlyInt(int v) : value{v} { print_created(this, value); }
MoveOnlyInt(MoveOnlyInt &&m) noexcept { MoveOnlyInt(MoveOnlyInt &&m) noexcept {
print_move_created(this, m.value); print_move_created(this, m.value);
std::swap(value, m.value); std::swap(value, m.value);
...@@ -56,7 +56,7 @@ public: ...@@ -56,7 +56,7 @@ public:
class MoveOrCopyInt { class MoveOrCopyInt {
public: public:
MoveOrCopyInt() { print_default_created(this); } MoveOrCopyInt() { print_default_created(this); }
MoveOrCopyInt(int v) : value{v} { print_created(this, value); } explicit MoveOrCopyInt(int v) : value{v} { print_created(this, value); }
MoveOrCopyInt(MoveOrCopyInt &&m) noexcept { MoveOrCopyInt(MoveOrCopyInt &&m) noexcept {
print_move_created(this, m.value); print_move_created(this, m.value);
std::swap(value, m.value); std::swap(value, m.value);
...@@ -75,7 +75,7 @@ public: ...@@ -75,7 +75,7 @@ public:
class CopyOnlyInt { class CopyOnlyInt {
public: public:
CopyOnlyInt() { print_default_created(this); } CopyOnlyInt() { print_default_created(this); }
CopyOnlyInt(int v) : value{v} { print_created(this, value); } explicit CopyOnlyInt(int v) : value{v} { print_created(this, value); }
CopyOnlyInt(const CopyOnlyInt &c) { print_copy_created(this, c.value); value = c.value; } CopyOnlyInt(const CopyOnlyInt &c) { print_copy_created(this, c.value); value = c.value; }
CopyOnlyInt &operator=(const CopyOnlyInt &c) { print_copy_assigned(this, c.value); value = c.value; return *this; } CopyOnlyInt &operator=(const CopyOnlyInt &c) { print_copy_assigned(this, c.value); value = c.value; return *this; }
~CopyOnlyInt() { print_destroyed(this); } ~CopyOnlyInt() { print_destroyed(this); }
...@@ -107,8 +107,8 @@ public: ...@@ -107,8 +107,8 @@ public:
if (!src) return none().release(); if (!src) return none().release();
return cast(*src, policy, parent); return cast(*src, policy, parent);
} }
operator CopyOnlyInt*() { return &value; } explicit operator CopyOnlyInt *() { return &value; }
operator CopyOnlyInt&() { return value; } explicit operator CopyOnlyInt &() { return value; }
template <typename T> using cast_op_type = pybind11::detail::cast_op_type<T>; template <typename T> using cast_op_type = pybind11::detail::cast_op_type<T>;
}; };
PYBIND11_NAMESPACE_END(detail) PYBIND11_NAMESPACE_END(detail)
...@@ -219,7 +219,7 @@ TEST_SUBMODULE(copy_move_policies, m) { ...@@ -219,7 +219,7 @@ TEST_SUBMODULE(copy_move_policies, m) {
// #389: rvp::move should fall-through to copy on non-movable objects // #389: rvp::move should fall-through to copy on non-movable objects
struct MoveIssue1 { struct MoveIssue1 {
int v; int v;
MoveIssue1(int v) : v{v} {} explicit MoveIssue1(int v) : v{v} {}
MoveIssue1(const MoveIssue1 &c) = default; MoveIssue1(const MoveIssue1 &c) = default;
MoveIssue1(MoveIssue1 &&) = delete; MoveIssue1(MoveIssue1 &&) = delete;
}; };
...@@ -227,7 +227,7 @@ TEST_SUBMODULE(copy_move_policies, m) { ...@@ -227,7 +227,7 @@ TEST_SUBMODULE(copy_move_policies, m) {
struct MoveIssue2 { struct MoveIssue2 {
int v; int v;
MoveIssue2(int v) : v{v} {} explicit MoveIssue2(int v) : v{v} {}
MoveIssue2(MoveIssue2 &&) = default; MoveIssue2(MoveIssue2 &&) = default;
}; };
py::class_<MoveIssue2>(m, "MoveIssue2").def(py::init<int>()).def_readwrite("value", &MoveIssue2::v); py::class_<MoveIssue2>(m, "MoveIssue2").def(py::init<int>()).def_readwrite("value", &MoveIssue2::v);
......
...@@ -9,7 +9,7 @@ namespace py = pybind11; ...@@ -9,7 +9,7 @@ namespace py = pybind11;
PYBIND11_MODULE(external_module, m) { PYBIND11_MODULE(external_module, m) {
class A { class A {
public: public:
A(int value) : v{value} {}; explicit A(int value) : v{value} {};
int v; int v;
}; };
......
...@@ -18,7 +18,7 @@ using namespace py::literals; ...@@ -18,7 +18,7 @@ using namespace py::literals;
class Widget { class Widget {
public: public:
Widget(std::string message) : message(std::move(message)) {} explicit Widget(std::string message) : message(std::move(message)) {}
virtual ~Widget() = default; virtual ~Widget() = default;
std::string the_message() const { return message; } std::string the_message() const { return message; }
......
...@@ -81,7 +81,7 @@ private: ...@@ -81,7 +81,7 @@ private:
struct PythonCallInDestructor { struct PythonCallInDestructor {
PythonCallInDestructor(const py::dict &d) : d(d) {} explicit PythonCallInDestructor(const py::dict &d) : d(d) {}
~PythonCallInDestructor() { d["good"] = true; } ~PythonCallInDestructor() { d["good"] = true; }
py::dict d; py::dict d;
...@@ -90,7 +90,7 @@ struct PythonCallInDestructor { ...@@ -90,7 +90,7 @@ struct PythonCallInDestructor {
struct PythonAlreadySetInDestructor { struct PythonAlreadySetInDestructor {
PythonAlreadySetInDestructor(const py::str &s) : s(s) {} explicit PythonAlreadySetInDestructor(const py::str &s) : s(s) {}
~PythonAlreadySetInDestructor() { ~PythonAlreadySetInDestructor() {
py::dict foo; py::dict foo;
try { try {
......
...@@ -19,8 +19,9 @@ ...@@ -19,8 +19,9 @@
class TestFactory1 { class TestFactory1 {
friend class TestFactoryHelper; friend class TestFactoryHelper;
TestFactory1() : value("(empty)") { print_default_created(this); } TestFactory1() : value("(empty)") { print_default_created(this); }
TestFactory1(int v) : value(std::to_string(v)) { print_created(this, value); } explicit TestFactory1(int v) : value(std::to_string(v)) { print_created(this, value); }
TestFactory1(std::string v) : value(std::move(v)) { print_created(this, value); } explicit TestFactory1(std::string v) : value(std::move(v)) { print_created(this, value); }
public: public:
std::string value; std::string value;
TestFactory1(TestFactory1 &&) = delete; TestFactory1(TestFactory1 &&) = delete;
...@@ -33,8 +34,9 @@ public: ...@@ -33,8 +34,9 @@ public:
class TestFactory2 { class TestFactory2 {
friend class TestFactoryHelper; friend class TestFactoryHelper;
TestFactory2() : value("(empty2)") { print_default_created(this); } TestFactory2() : value("(empty2)") { print_default_created(this); }
TestFactory2(int v) : value(std::to_string(v)) { print_created(this, value); } explicit TestFactory2(int v) : value(std::to_string(v)) { print_created(this, value); }
TestFactory2(std::string v) : value(std::move(v)) { print_created(this, value); } explicit TestFactory2(std::string v) : value(std::move(v)) { print_created(this, value); }
public: public:
TestFactory2(TestFactory2 &&m) noexcept { TestFactory2(TestFactory2 &&m) noexcept {
value = std::move(m.value); value = std::move(m.value);
...@@ -53,9 +55,10 @@ class TestFactory3 { ...@@ -53,9 +55,10 @@ class TestFactory3 {
protected: protected:
friend class TestFactoryHelper; friend class TestFactoryHelper;
TestFactory3() : value("(empty3)") { print_default_created(this); } TestFactory3() : value("(empty3)") { print_default_created(this); }
TestFactory3(int v) : value(std::to_string(v)) { print_created(this, value); } explicit TestFactory3(int v) : value(std::to_string(v)) { print_created(this, value); }
public: public:
TestFactory3(std::string v) : value(std::move(v)) { print_created(this, value); } explicit TestFactory3(std::string v) : value(std::move(v)) { print_created(this, value); }
TestFactory3(TestFactory3 &&m) noexcept { TestFactory3(TestFactory3 &&m) noexcept {
value = std::move(m.value); value = std::move(m.value);
print_move_created(this); print_move_created(this);
...@@ -72,13 +75,13 @@ public: ...@@ -72,13 +75,13 @@ public:
class TestFactory4 : public TestFactory3 { class TestFactory4 : public TestFactory3 {
public: public:
TestFactory4() : TestFactory3() { print_default_created(this); } TestFactory4() : TestFactory3() { print_default_created(this); }
TestFactory4(int v) : TestFactory3(v) { print_created(this, v); } explicit TestFactory4(int v) : TestFactory3(v) { print_created(this, v); }
~TestFactory4() override { print_destroyed(this); } ~TestFactory4() override { print_destroyed(this); }
}; };
// Another class for an invalid downcast test // Another class for an invalid downcast test
class TestFactory5 : public TestFactory3 { class TestFactory5 : public TestFactory3 {
public: public:
TestFactory5(int i) : TestFactory3(i) { print_created(this, i); } explicit TestFactory5(int i) : TestFactory3(i) { print_created(this, i); }
~TestFactory5() override { print_destroyed(this); } ~TestFactory5() override { print_destroyed(this); }
}; };
...@@ -87,7 +90,7 @@ protected: ...@@ -87,7 +90,7 @@ protected:
int value; int value;
bool alias = false; bool alias = false;
public: public:
TestFactory6(int i) : value{i} { print_created(this, i); } explicit TestFactory6(int i) : value{i} { print_created(this, i); }
TestFactory6(TestFactory6 &&f) noexcept { TestFactory6(TestFactory6 &&f) noexcept {
print_move_created(this); print_move_created(this);
value = f.value; value = f.value;
...@@ -102,11 +105,20 @@ class PyTF6 : public TestFactory6 { ...@@ -102,11 +105,20 @@ class PyTF6 : public TestFactory6 {
public: public:
// Special constructor that allows the factory to construct a PyTF6 from a TestFactory6 only // Special constructor that allows the factory to construct a PyTF6 from a TestFactory6 only
// when an alias is needed: // when an alias is needed:
PyTF6(TestFactory6 &&base) : TestFactory6(std::move(base)) { alias = true; print_created(this, "move", value); } explicit PyTF6(TestFactory6 &&base) : TestFactory6(std::move(base)) {
PyTF6(int i) : TestFactory6(i) { alias = true; print_created(this, i); } alias = true;
print_created(this, "move", value);
}
explicit PyTF6(int i) : TestFactory6(i) {
alias = true;
print_created(this, i);
}
PyTF6(PyTF6 &&f) noexcept : TestFactory6(std::move(f)) { print_move_created(this); } PyTF6(PyTF6 &&f) noexcept : TestFactory6(std::move(f)) { print_move_created(this); }
PyTF6(const PyTF6 &f) : TestFactory6(f) { print_copy_created(this); } PyTF6(const PyTF6 &f) : TestFactory6(f) { print_copy_created(this); }
PyTF6(std::string s) : TestFactory6((int) s.size()) { alias = true; print_created(this, s); } explicit PyTF6(std::string s) : TestFactory6((int) s.size()) {
alias = true;
print_created(this, s);
}
~PyTF6() override { print_destroyed(this); } ~PyTF6() override { print_destroyed(this); }
int get() override { PYBIND11_OVERRIDE(int, TestFactory6, get, /*no args*/); } int get() override { PYBIND11_OVERRIDE(int, TestFactory6, get, /*no args*/); }
}; };
...@@ -116,7 +128,7 @@ protected: ...@@ -116,7 +128,7 @@ protected:
int value; int value;
bool alias = false; bool alias = false;
public: public:
TestFactory7(int i) : value{i} { print_created(this, i); } explicit TestFactory7(int i) : value{i} { print_created(this, i); }
TestFactory7(TestFactory7 &&f) noexcept { TestFactory7(TestFactory7 &&f) noexcept {
print_move_created(this); print_move_created(this);
value = f.value; value = f.value;
...@@ -129,7 +141,10 @@ public: ...@@ -129,7 +141,10 @@ public:
}; };
class PyTF7 : public TestFactory7 { class PyTF7 : public TestFactory7 {
public: public:
PyTF7(int i) : TestFactory7(i) { alias = true; print_created(this, i); } explicit PyTF7(int i) : TestFactory7(i) {
alias = true;
print_created(this, i);
}
PyTF7(PyTF7 &&f) noexcept : TestFactory7(std::move(f)) { print_move_created(this); } PyTF7(PyTF7 &&f) noexcept : TestFactory7(std::move(f)) { print_move_created(this); }
PyTF7(const PyTF7 &f) : TestFactory7(f) { print_copy_created(this); } PyTF7(const PyTF7 &f) : TestFactory7(f) { print_copy_created(this); }
~PyTF7() override { print_destroyed(this); } ~PyTF7() override { print_destroyed(this); }
...@@ -300,7 +315,7 @@ TEST_SUBMODULE(factory_constructors, m) { ...@@ -300,7 +315,7 @@ TEST_SUBMODULE(factory_constructors, m) {
// Class with a custom new operator but *without* a placement new operator (issue #948) // Class with a custom new operator but *without* a placement new operator (issue #948)
class NoPlacementNew { class NoPlacementNew {
public: public:
NoPlacementNew(int i) : i(i) { } explicit NoPlacementNew(int i) : i(i) {}
static void *operator new(std::size_t s) { static void *operator new(std::size_t s) {
auto *p = ::operator new(s); auto *p = ::operator new(s);
py::print("operator new called, returning", reinterpret_cast<uintptr_t>(p)); py::print("operator new called, returning", reinterpret_cast<uintptr_t>(p));
...@@ -324,8 +339,8 @@ TEST_SUBMODULE(factory_constructors, m) { ...@@ -324,8 +339,8 @@ TEST_SUBMODULE(factory_constructors, m) {
// Class that has verbose operator_new/operator_delete calls // Class that has verbose operator_new/operator_delete calls
struct NoisyAlloc { struct NoisyAlloc {
NoisyAlloc(const NoisyAlloc &) = default; NoisyAlloc(const NoisyAlloc &) = default;
NoisyAlloc(int i) { py::print(py::str("NoisyAlloc(int {})").format(i)); } explicit NoisyAlloc(int i) { py::print(py::str("NoisyAlloc(int {})").format(i)); }
NoisyAlloc(double d) { py::print(py::str("NoisyAlloc(double {})").format(d)); } explicit NoisyAlloc(double d) { py::print(py::str("NoisyAlloc(double {})").format(d)); }
~NoisyAlloc() { py::print("~NoisyAlloc()"); } ~NoisyAlloc() { py::print("~NoisyAlloc()"); }
static void *operator new(size_t s) { py::print("noisy new"); return ::operator new(s); } static void *operator new(size_t s) { py::print("noisy new"); return ::operator new(s); }
......
...@@ -91,7 +91,7 @@ TEST_SUBMODULE(local_bindings, m) { ...@@ -91,7 +91,7 @@ TEST_SUBMODULE(local_bindings, m) {
class Cat : public pets::Pet { class Cat : public pets::Pet {
public: public:
Cat(std::string name) : Pet(std::move(name)) {} explicit Cat(std::string name) : Pet(std::move(name)) {}
}; };
py::class_<pets::Pet>(m, "Pet", py::module_local()) py::class_<pets::Pet>(m, "Pet", py::module_local())
.def("get_name", &pets::Pet::name); .def("get_name", &pets::Pet::name);
......
...@@ -19,9 +19,9 @@ using overload_cast_ = pybind11::detail::overload_cast_impl<Args...>; ...@@ -19,9 +19,9 @@ using overload_cast_ = pybind11::detail::overload_cast_impl<Args...>;
class ExampleMandA { class ExampleMandA {
public: public:
ExampleMandA() { print_default_created(this); } ExampleMandA() { print_default_created(this); }
ExampleMandA(int value) : value(value) { print_created(this, value); } explicit ExampleMandA(int value) : value(value) { print_created(this, value); }
ExampleMandA(const ExampleMandA &e) : value(e.value) { print_copy_created(this); } ExampleMandA(const ExampleMandA &e) : value(e.value) { print_copy_created(this); }
ExampleMandA(std::string&&) {} explicit ExampleMandA(std::string &&) {}
ExampleMandA(ExampleMandA &&e) noexcept : value(e.value) { print_move_created(this); } ExampleMandA(ExampleMandA &&e) noexcept : value(e.value) { print_move_created(this); }
~ExampleMandA() { print_destroyed(this); } ~ExampleMandA() { print_destroyed(this); }
...@@ -124,14 +124,14 @@ class NoneCastTester { ...@@ -124,14 +124,14 @@ class NoneCastTester {
public: public:
int answer = -1; int answer = -1;
NoneCastTester() = default; NoneCastTester() = default;
NoneCastTester(int v) : answer(v) {} explicit NoneCastTester(int v) : answer(v) {}
}; };
struct StrIssue { struct StrIssue {
int val = -1; int val = -1;
StrIssue() = default; StrIssue() = default;
StrIssue(int i) : val{i} {} explicit StrIssue(int i) : val{i} {}
}; };
// Issues #854, #910: incompatible function args when member function/pointer is in unregistered base class // Issues #854, #910: incompatible function args when member function/pointer is in unregistered base class
......
...@@ -20,7 +20,7 @@ TEST_SUBMODULE(modules, m) { ...@@ -20,7 +20,7 @@ TEST_SUBMODULE(modules, m) {
// test_reference_internal // test_reference_internal
class A { class A {
public: public:
A(int v) : v(v) { print_created(this, v); } explicit A(int v) : v(v) { print_created(this, v); }
~A() { print_destroyed(this); } ~A() { print_destroyed(this); }
A(const A&) { print_copy_created(this); } A(const A&) { print_copy_created(this); }
A& operator=(const A &copy) { print_copy_assigned(this); v = copy.v; return *this; } A& operator=(const A &copy) { print_copy_assigned(this); v = copy.v; return *this; }
......
...@@ -16,7 +16,7 @@ namespace { ...@@ -16,7 +16,7 @@ namespace {
// Many bases for testing that multiple inheritance from many classes (i.e. requiring extra // Many bases for testing that multiple inheritance from many classes (i.e. requiring extra
// space for holder constructed flags) works. // space for holder constructed flags) works.
template <int N> struct BaseN { template <int N> struct BaseN {
BaseN(int i) : i(i) { } explicit BaseN(int i) : i(i) {}
int i; int i;
}; };
...@@ -47,12 +47,12 @@ int VanillaStaticMix2::static_value = 12; ...@@ -47,12 +47,12 @@ int VanillaStaticMix2::static_value = 12;
// test_multiple_inheritance_virtbase // test_multiple_inheritance_virtbase
struct Base1a { struct Base1a {
Base1a(int i) : i(i) { } explicit Base1a(int i) : i(i) {}
int foo() const { return i; } int foo() const { return i; }
int i; int i;
}; };
struct Base2a { struct Base2a {
Base2a(int i) : i(i) { } explicit Base2a(int i) : i(i) {}
int bar() const { return i; } int bar() const { return i; }
int i; int i;
}; };
...@@ -77,7 +77,7 @@ TEST_SUBMODULE(multiple_inheritance, m) { ...@@ -77,7 +77,7 @@ TEST_SUBMODULE(multiple_inheritance, m) {
// test_multiple_inheritance_mix1 // test_multiple_inheritance_mix1
// test_multiple_inheritance_mix2 // test_multiple_inheritance_mix2
struct Base1 { struct Base1 {
Base1(int i) : i(i) { } explicit Base1(int i) : i(i) {}
int foo() const { return i; } int foo() const { return i; }
int i; int i;
}; };
...@@ -86,7 +86,7 @@ TEST_SUBMODULE(multiple_inheritance, m) { ...@@ -86,7 +86,7 @@ TEST_SUBMODULE(multiple_inheritance, m) {
.def("foo", &Base1::foo); .def("foo", &Base1::foo);
struct Base2 { struct Base2 {
Base2(int i) : i(i) { } explicit Base2(int i) : i(i) {}
int bar() const { return i; } int bar() const { return i; }
int i; int i;
}; };
......
...@@ -52,7 +52,7 @@ TEST_SUBMODULE(numpy_vectorize, m) { ...@@ -52,7 +52,7 @@ TEST_SUBMODULE(numpy_vectorize, m) {
// Passthrough test: references and non-pod types should be automatically passed through (in the // Passthrough test: references and non-pod types should be automatically passed through (in the
// function definition below, only `b`, `d`, and `g` are vectorized): // function definition below, only `b`, `d`, and `g` are vectorized):
struct NonPODClass { struct NonPODClass {
NonPODClass(int v) : value{v} {} explicit NonPODClass(int v) : value{v} {}
int value; int value;
}; };
py::class_<NonPODClass>(m, "NonPODClass") py::class_<NonPODClass>(m, "NonPODClass")
...@@ -71,7 +71,7 @@ TEST_SUBMODULE(numpy_vectorize, m) { ...@@ -71,7 +71,7 @@ TEST_SUBMODULE(numpy_vectorize, m) {
// test_method_vectorization // test_method_vectorization
struct VectorizeTestClass { struct VectorizeTestClass {
VectorizeTestClass(int v) : value{v} {}; explicit VectorizeTestClass(int v) : value{v} {};
float method(int x, float y) const { return y + (float) (x + value); } float method(int x, float y) const { return y + (float) (x + value); }
int value = 0; int value = 0;
}; };
......
...@@ -67,7 +67,7 @@ TEST_SUBMODULE(pickling, m) { ...@@ -67,7 +67,7 @@ TEST_SUBMODULE(pickling, m) {
// test_roundtrip // test_roundtrip
class Pickleable { class Pickleable {
public: public:
Pickleable(const std::string &value) : m_value(value) { } explicit Pickleable(const std::string &value) : m_value(value) { }
const std::string &value() const { return m_value; } const std::string &value() const { return m_value; }
void setExtra1(int extra1) { m_extra1 = extra1; } void setExtra1(int extra1) { m_extra1 = extra1; }
...@@ -132,7 +132,7 @@ TEST_SUBMODULE(pickling, m) { ...@@ -132,7 +132,7 @@ TEST_SUBMODULE(pickling, m) {
// test_roundtrip_with_dict // test_roundtrip_with_dict
class PickleableWithDict { class PickleableWithDict {
public: public:
PickleableWithDict(const std::string &value) : value(value) { } explicit PickleableWithDict(const std::string &value) : value(value) { }
std::string value; std::string value;
int extra; int extra;
......
...@@ -20,7 +20,7 @@ template<typename T> ...@@ -20,7 +20,7 @@ template<typename T>
class NonZeroIterator { class NonZeroIterator {
const T* ptr_; const T* ptr_;
public: public:
NonZeroIterator(const T* ptr) : ptr_(ptr) {} explicit NonZeroIterator(const T *ptr) : ptr_(ptr) {}
const T& operator*() const { return *ptr_; } const T& operator*() const { return *ptr_; }
NonZeroIterator& operator++() { ++ptr_; return *this; } NonZeroIterator& operator++() { ++ptr_; return *this; }
}; };
...@@ -77,9 +77,9 @@ TEST_SUBMODULE(sequences_and_iterators, m) { ...@@ -77,9 +77,9 @@ TEST_SUBMODULE(sequences_and_iterators, m) {
// test_sliceable // test_sliceable
class Sliceable{ class Sliceable{
public: public:
Sliceable(int n): size(n) {} explicit Sliceable(int n) : size(n) {}
int start,stop,step; int start, stop, step;
int size; int size;
}; };
py::class_<Sliceable>(m, "Sliceable") py::class_<Sliceable>(m, "Sliceable")
.def(py::init<int>()) .def(py::init<int>())
...@@ -96,12 +96,12 @@ TEST_SUBMODULE(sequences_and_iterators, m) { ...@@ -96,12 +96,12 @@ TEST_SUBMODULE(sequences_and_iterators, m) {
// test_sequence // test_sequence
class Sequence { class Sequence {
public: public:
Sequence(size_t size) : m_size(size) { explicit Sequence(size_t size) : m_size(size) {
print_created(this, "of size", m_size); print_created(this, "of size", m_size);
m_data = new float[size]; m_data = new float[size];
memset(m_data, 0, sizeof(float) * size); memset(m_data, 0, sizeof(float) * size);
} }
Sequence(const std::vector<float> &value) : m_size(value.size()) { explicit Sequence(const std::vector<float> &value) : m_size(value.size()) {
print_created(this, "of size", m_size, "from std::vector"); print_created(this, "of size", m_size, "from std::vector");
m_data = new float[m_size]; m_data = new float[m_size];
memcpy(m_data, &value[0], sizeof(float) * m_size); memcpy(m_data, &value[0], sizeof(float) * m_size);
...@@ -239,7 +239,7 @@ TEST_SUBMODULE(sequences_and_iterators, m) { ...@@ -239,7 +239,7 @@ TEST_SUBMODULE(sequences_and_iterators, m) {
class StringMap { class StringMap {
public: public:
StringMap() = default; StringMap() = default;
StringMap(std::unordered_map<std::string, std::string> init) explicit StringMap(std::unordered_map<std::string, std::string> init)
: map(std::move(init)) {} : map(std::move(init)) {}
void set(const std::string &key, std::string val) { map[key] = std::move(val); } void set(const std::string &key, std::string val) { map[key] = std::move(val); }
...@@ -276,7 +276,7 @@ TEST_SUBMODULE(sequences_and_iterators, m) { ...@@ -276,7 +276,7 @@ TEST_SUBMODULE(sequences_and_iterators, m) {
// test_generalized_iterators // test_generalized_iterators
class IntPairs { class IntPairs {
public: public:
IntPairs(std::vector<std::pair<int, int>> data) : data_(std::move(data)) {} explicit IntPairs(std::vector<std::pair<int, int>> data) : data_(std::move(data)) {}
const std::pair<int, int>* begin() const { return data_.data(); } const std::pair<int, int>* begin() const { return data_.data(); }
private: private:
std::vector<std::pair<int, int>> data_; std::vector<std::pair<int, int>> data_;
......
...@@ -24,7 +24,7 @@ template <typename T> class huge_unique_ptr { ...@@ -24,7 +24,7 @@ template <typename T> class huge_unique_ptr {
std::unique_ptr<T> ptr; std::unique_ptr<T> ptr;
uint64_t padding[10]; uint64_t padding[10];
public: public:
huge_unique_ptr(T *p) : ptr(p) {} explicit huge_unique_ptr(T *p) : ptr(p) {}
T *get() { return ptr.get(); } T *get() { return ptr.get(); }
}; };
...@@ -33,7 +33,7 @@ template <typename T> ...@@ -33,7 +33,7 @@ template <typename T>
class custom_unique_ptr { class custom_unique_ptr {
std::unique_ptr<T> impl; std::unique_ptr<T> impl;
public: public:
custom_unique_ptr(T* p) : impl(p) { } explicit custom_unique_ptr(T *p) : impl(p) {}
T* get() const { return impl.get(); } T* get() const { return impl.get(); }
T* release_ptr() { return impl.release(); } T* release_ptr() { return impl.release(); }
}; };
...@@ -46,7 +46,7 @@ class shared_ptr_with_addressof_operator { ...@@ -46,7 +46,7 @@ class shared_ptr_with_addressof_operator {
std::shared_ptr<T> impl; std::shared_ptr<T> impl;
public: public:
shared_ptr_with_addressof_operator( ) = default; shared_ptr_with_addressof_operator( ) = default;
shared_ptr_with_addressof_operator(T* p) : impl(p) { } explicit shared_ptr_with_addressof_operator(T *p) : impl(p) {}
T* get() const { return impl.get(); } T* get() const { return impl.get(); }
T** operator&() { throw std::logic_error("Call of overloaded operator& is not expected"); } T** operator&() { throw std::logic_error("Call of overloaded operator& is not expected"); }
}; };
...@@ -59,7 +59,7 @@ class unique_ptr_with_addressof_operator { ...@@ -59,7 +59,7 @@ class unique_ptr_with_addressof_operator {
std::unique_ptr<T> impl; std::unique_ptr<T> impl;
public: public:
unique_ptr_with_addressof_operator() = default; unique_ptr_with_addressof_operator() = default;
unique_ptr_with_addressof_operator(T* p) : impl(p) { } explicit unique_ptr_with_addressof_operator(T *p) : impl(p) {}
T* get() const { return impl.get(); } T* get() const { return impl.get(); }
T* release_ptr() { return impl.release(); } T* release_ptr() { return impl.release(); }
T** operator&() { throw std::logic_error("Call of overloaded operator& is not expected"); } T** operator&() { throw std::logic_error("Call of overloaded operator& is not expected"); }
...@@ -68,7 +68,7 @@ public: ...@@ -68,7 +68,7 @@ public:
// Custom object with builtin reference counting (see 'object.h' for the implementation) // Custom object with builtin reference counting (see 'object.h' for the implementation)
class MyObject1 : public Object { class MyObject1 : public Object {
public: public:
MyObject1(int value) : value(value) { print_created(this, toString()); } explicit MyObject1(int value) : value(value) { print_created(this, toString()); }
std::string toString() const override { return "MyObject1[" + std::to_string(value) + "]"; } std::string toString() const override { return "MyObject1[" + std::to_string(value) + "]"; }
protected: protected:
~MyObject1() override { print_destroyed(this); } ~MyObject1() override { print_destroyed(this); }
...@@ -80,7 +80,7 @@ private: ...@@ -80,7 +80,7 @@ private:
class MyObject2 { class MyObject2 {
public: public:
MyObject2(const MyObject2 &) = default; MyObject2(const MyObject2 &) = default;
MyObject2(int value) : value(value) { print_created(this, toString()); } explicit MyObject2(int value) : value(value) { print_created(this, toString()); }
std::string toString() const { return "MyObject2[" + std::to_string(value) + "]"; } std::string toString() const { return "MyObject2[" + std::to_string(value) + "]"; }
virtual ~MyObject2() { print_destroyed(this); } virtual ~MyObject2() { print_destroyed(this); }
private: private:
...@@ -91,7 +91,7 @@ private: ...@@ -91,7 +91,7 @@ private:
class MyObject3 : public std::enable_shared_from_this<MyObject3> { class MyObject3 : public std::enable_shared_from_this<MyObject3> {
public: public:
MyObject3(const MyObject3 &) = default; MyObject3(const MyObject3 &) = default;
MyObject3(int value) : value(value) { print_created(this, toString()); } explicit MyObject3(int value) : value(value) { print_created(this, toString()); }
std::string toString() const { return "MyObject3[" + std::to_string(value) + "]"; } std::string toString() const { return "MyObject3[" + std::to_string(value) + "]"; }
virtual ~MyObject3() { print_destroyed(this); } virtual ~MyObject3() { print_destroyed(this); }
private: private:
...@@ -104,7 +104,7 @@ class MyObject4; ...@@ -104,7 +104,7 @@ class MyObject4;
std::unordered_set<MyObject4 *> myobject4_instances; std::unordered_set<MyObject4 *> myobject4_instances;
class MyObject4 { class MyObject4 {
public: public:
MyObject4(int value) : value{value} { explicit MyObject4(int value) : value{value} {
print_created(this); print_created(this);
myobject4_instances.insert(this); myobject4_instances.insert(this);
} }
...@@ -130,7 +130,7 @@ class MyObject4a; ...@@ -130,7 +130,7 @@ class MyObject4a;
std::unordered_set<MyObject4a *> myobject4a_instances; std::unordered_set<MyObject4a *> myobject4a_instances;
class MyObject4a { class MyObject4a {
public: public:
MyObject4a(int i) { explicit MyObject4a(int i) {
value = i; value = i;
print_created(this); print_created(this);
myobject4a_instances.insert(this); myobject4a_instances.insert(this);
...@@ -153,14 +153,14 @@ protected: ...@@ -153,14 +153,14 @@ protected:
// Object derived but with public destructor and no Deleter in default holder // Object derived but with public destructor and no Deleter in default holder
class MyObject4b : public MyObject4a { class MyObject4b : public MyObject4a {
public: public:
MyObject4b(int i) : MyObject4a(i) { print_created(this); } explicit MyObject4b(int i) : MyObject4a(i) { print_created(this); }
~MyObject4b() override { print_destroyed(this); } ~MyObject4b() override { print_destroyed(this); }
}; };
// test_large_holder // test_large_holder
class MyObject5 { // managed by huge_unique_ptr class MyObject5 { // managed by huge_unique_ptr
public: public:
MyObject5(int value) : value{value} { print_created(this); } explicit MyObject5(int value) : value{value} { print_created(this); }
~MyObject5() { print_destroyed(this); } ~MyObject5() { print_destroyed(this); }
int value; int value;
}; };
...@@ -222,7 +222,7 @@ struct TypeForHolderWithAddressOf { ...@@ -222,7 +222,7 @@ struct TypeForHolderWithAddressOf {
// test_move_only_holder_with_addressof_operator // test_move_only_holder_with_addressof_operator
struct TypeForMoveOnlyHolderWithAddressOf { struct TypeForMoveOnlyHolderWithAddressOf {
TypeForMoveOnlyHolderWithAddressOf(int value) : value{value} { print_created(this); } explicit TypeForMoveOnlyHolderWithAddressOf(int value) : value{value} { print_created(this); }
~TypeForMoveOnlyHolderWithAddressOf() { print_destroyed(this); } ~TypeForMoveOnlyHolderWithAddressOf() { print_destroyed(this); }
std::string toString() const { std::string toString() const {
return "MoveOnlyHolderWithAddressOf[" + std::to_string(value) + "]"; return "MoveOnlyHolderWithAddressOf[" + std::to_string(value) + "]";
...@@ -242,7 +242,7 @@ struct ElementBase { ...@@ -242,7 +242,7 @@ struct ElementBase {
}; };
struct ElementA : ElementBase { struct ElementA : ElementBase {
ElementA(int v) : v(v) { } explicit ElementA(int v) : v(v) {}
int value() const { return v; } int value() const { return v; }
int v; int v;
}; };
...@@ -291,9 +291,9 @@ TEST_SUBMODULE(smart_ptr, m) { ...@@ -291,9 +291,9 @@ TEST_SUBMODULE(smart_ptr, m) {
py::implicitly_convertible<py::int_, MyObject1>(); py::implicitly_convertible<py::int_, MyObject1>();
m.def("make_object_1", []() -> Object * { return new MyObject1(1); }); m.def("make_object_1", []() -> Object * { return new MyObject1(1); });
m.def("make_object_2", []() -> ref<Object> { return new MyObject1(2); }); m.def("make_object_2", []() -> ref<Object> { return ref<Object>(new MyObject1(2)); });
m.def("make_myobject1_1", []() -> MyObject1 * { return new MyObject1(4); }); m.def("make_myobject1_1", []() -> MyObject1 * { return new MyObject1(4); });
m.def("make_myobject1_2", []() -> ref<MyObject1> { return new MyObject1(5); }); m.def("make_myobject1_2", []() -> ref<MyObject1> { return ref<MyObject1>(new MyObject1(5)); });
m.def("print_object_1", [](const Object *obj) { py::print(obj->toString()); }); m.def("print_object_1", [](const Object *obj) { py::print(obj->toString()); });
m.def("print_object_2", [](ref<Object> obj) { py::print(obj->toString()); }); m.def("print_object_2", [](ref<Object> obj) { py::print(obj->toString()); });
m.def("print_object_3", [](const ref<Object> &obj) { py::print(obj->toString()); }); m.def("print_object_3", [](const ref<Object> &obj) { py::print(obj->toString()); });
...@@ -328,7 +328,7 @@ TEST_SUBMODULE(smart_ptr, m) { ...@@ -328,7 +328,7 @@ TEST_SUBMODULE(smart_ptr, m) {
// test_smart_ptr_refcounting // test_smart_ptr_refcounting
m.def("test_object1_refcounting", []() { m.def("test_object1_refcounting", []() {
ref<MyObject1> o = new MyObject1(0); auto o = ref<MyObject1>(new MyObject1(0));
bool good = o->getRefCount() == 1; bool good = o->getRefCount() == 1;
py::object o2 = py::cast(o, py::return_value_policy::reference); py::object o2 = py::cast(o, py::return_value_policy::reference);
// always request (partial) ownership for objects with intrusive // always request (partial) ownership for objects with intrusive
......
...@@ -45,7 +45,8 @@ PYBIND11_MAKE_OPAQUE(std::vector<std::string, std::allocator<std::string>>); ...@@ -45,7 +45,8 @@ PYBIND11_MAKE_OPAQUE(std::vector<std::string, std::allocator<std::string>>);
/// Issue #528: templated constructor /// Issue #528: templated constructor
struct TplCtorClass { struct TplCtorClass {
template <typename T> TplCtorClass(const T &) { } template <typename T>
explicit TplCtorClass(const T &) {}
bool operator==(const TplCtorClass &) const { return true; } bool operator==(const TplCtorClass &) const { return true; }
}; };
......
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
class El { class El {
public: public:
El() = delete; El() = delete;
El(int v) : a(v) { } explicit El(int v) : a(v) {}
int a; int a;
}; };
......
...@@ -37,33 +37,35 @@ struct Animal ...@@ -37,33 +37,35 @@ struct Animal
struct Dog : Animal struct Dog : Animal
{ {
Dog(const std::string& _name, Kind _kind = Kind::Dog) : Animal(_name, _kind) {} explicit Dog(const std::string &_name, Kind _kind = Kind::Dog) : Animal(_name, _kind) {}
std::string bark() const { return name_of_kind(kind) + " " + name + " goes " + sound; } std::string bark() const { return name_of_kind(kind) + " " + name + " goes " + sound; }
std::string sound = "WOOF!"; std::string sound = "WOOF!";
}; };
struct Labrador : Dog struct Labrador : Dog
{ {
Labrador(const std::string& _name, int _excitement = 9001) explicit Labrador(const std::string &_name, int _excitement = 9001)
: Dog(_name, Kind::Labrador), excitement(_excitement) {} : Dog(_name, Kind::Labrador), excitement(_excitement) {}
int excitement; int excitement;
}; };
struct Chihuahua : Dog struct Chihuahua : Dog
{ {
Chihuahua(const std::string& _name) : Dog(_name, Kind::Chihuahua) { sound = "iyiyiyiyiyi"; } explicit Chihuahua(const std::string &_name) : Dog(_name, Kind::Chihuahua) {
sound = "iyiyiyiyiyi";
}
std::string bark() const { return Dog::bark() + " and runs in circles"; } std::string bark() const { return Dog::bark() + " and runs in circles"; }
}; };
struct Cat : Animal struct Cat : Animal
{ {
Cat(const std::string& _name, Kind _kind = Kind::Cat) : Animal(_name, _kind) {} explicit Cat(const std::string &_name, Kind _kind = Kind::Cat) : Animal(_name, _kind) {}
std::string purr() const { return "mrowr"; } std::string purr() const { return "mrowr"; }
}; };
struct Panther : Cat struct Panther : Cat
{ {
Panther(const std::string& _name) : Cat(_name, Kind::Panther) {} explicit Panther(const std::string &_name) : Cat(_name, Kind::Panther) {}
std::string purr() const { return "mrrrRRRRRR"; } std::string purr() const { return "mrrrRRRRRR"; }
}; };
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
/* This is an example class that we'll want to be able to extend from Python */ /* This is an example class that we'll want to be able to extend from Python */
class ExampleVirt { class ExampleVirt {
public: public:
ExampleVirt(int state) : state(state) { print_created(this, state); } explicit ExampleVirt(int state) : state(state) { print_created(this, state); }
ExampleVirt(const ExampleVirt &e) : state(e.state) { print_copy_created(this); } ExampleVirt(const ExampleVirt &e) : state(e.state) { print_copy_created(this); }
ExampleVirt(ExampleVirt &&e) noexcept : state(e.state) { ExampleVirt(ExampleVirt &&e) noexcept : state(e.state) {
print_move_created(this); print_move_created(this);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment