diff --git a/include/pybind11/smart_holder.h b/include/pybind11/smart_holder.h index 0f9f6f4c0..25f0e6b52 100644 --- a/include/pybind11/smart_holder.h +++ b/include/pybind11/smart_holder.h @@ -5,3 +5,18 @@ #pragma once #include "detail/smart_holder_type_casters.h" +#include "pybind11.h" + +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) + +// Supports easier switching between py::class_ and py::class_: +// users can simply replace the `_` in `class_` with `h` or vice versa. +// Note though that the PYBIND11_SMART_HOLDER_TYPE_CASTERS(U) macro also needs to be +// added (for `classh`) or commented out (for `class_`). +template +class classh : public class_ { +public: + using class_::class_; +}; + +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/tests/class_sh_module_local_1.cpp b/tests/class_sh_module_local_1.cpp index 22adb733d..b8fd9591f 100644 --- a/tests/class_sh_module_local_1.cpp +++ b/tests/class_sh_module_local_1.cpp @@ -22,7 +22,7 @@ PYBIND11_MODULE(class_sh_module_local_1, m) { namespace py = pybind11; using namespace pybind11_tests::class_sh_module_local; - py::class_(m, "atyp", py::module_local()) + py::classh(m, "atyp", py::module_local()) .def(py::init([](const std::string &mtxt) { atyp obj; obj.mtxt = mtxt; diff --git a/tests/class_sh_module_local_2.cpp b/tests/class_sh_module_local_2.cpp index e6f866267..dd72cdcac 100644 --- a/tests/class_sh_module_local_2.cpp +++ b/tests/class_sh_module_local_2.cpp @@ -22,7 +22,7 @@ PYBIND11_MODULE(class_sh_module_local_2, m) { namespace py = pybind11; using namespace pybind11_tests::class_sh_module_local; - py::class_(m, "atyp", py::module_local()) + py::classh(m, "atyp", py::module_local()) .def(py::init([](const std::string &mtxt) { atyp obj; obj.mtxt = mtxt; diff --git a/tests/test_class_sh_basic.cpp b/tests/test_class_sh_basic.cpp index 488b033fc..96c9261fe 100644 --- a/tests/test_class_sh_basic.cpp +++ b/tests/test_class_sh_basic.cpp @@ -57,13 +57,11 @@ namespace class_sh_basic { TEST_SUBMODULE(class_sh_basic, m) { namespace py = pybind11; - py::class_(m, "atyp") - .def(py::init<>()) - .def(py::init([](const std::string &mtxt) { - atyp obj; - obj.mtxt = mtxt; - return obj; - })); + py::classh(m, "atyp").def(py::init<>()).def(py::init([](const std::string &mtxt) { + atyp obj; + obj.mtxt = mtxt; + return obj; + })); m.def("rtrn_valu_atyp", rtrn_valu_atyp); m.def("rtrn_rref_atyp", rtrn_rref_atyp); diff --git a/tests/test_class_sh_inheritance.cpp b/tests/test_class_sh_inheritance.cpp index 646b4fc31..1c825d7f0 100644 --- a/tests/test_class_sh_inheritance.cpp +++ b/tests/test_class_sh_inheritance.cpp @@ -73,8 +73,8 @@ namespace pybind11_tests { namespace class_sh_inheritance { TEST_SUBMODULE(class_sh_inheritance, m) { - py::class_(m, "base"); - py::class_(m, "drvd"); + py::classh(m, "base"); + py::classh(m, "drvd"); auto rvto = py::return_value_policy::take_ownership; @@ -89,9 +89,9 @@ TEST_SUBMODULE(class_sh_inheritance, m) { m.def("pass_shcp_drvd", pass_shcp_drvd); // __init__ needed for Python inheritance. - py::class_(m, "base1").def(py::init<>()); - py::class_(m, "base2").def(py::init<>()); - py::class_(m, "drvd2"); + py::classh(m, "base1").def(py::init<>()); + py::classh(m, "base2").def(py::init<>()); + py::classh(m, "drvd2"); m.def("rtrn_mptr_drvd2", rtrn_mptr_drvd2, rvto); m.def("rtrn_mptr_drvd2_up_cast1", rtrn_mptr_drvd2_up_cast1, rvto); diff --git a/tests/test_class_sh_unique_ptr_member.cpp b/tests/test_class_sh_unique_ptr_member.cpp index 0da05f4e3..a34274a0c 100644 --- a/tests/test_class_sh_unique_ptr_member.cpp +++ b/tests/test_class_sh_unique_ptr_member.cpp @@ -46,9 +46,7 @@ namespace pybind11_tests { namespace class_sh_unique_ptr_member { TEST_SUBMODULE(class_sh_unique_ptr_member, m) { - py::class_(m, "pointee") - .def(py::init<>()) - .def("get_int", &pointee::get_int); + py::classh(m, "pointee").def(py::init<>()).def("get_int", &pointee::get_int); m.def("make_unique_pointee", make_unique_pointee);