14 #if !defined(PYBIND11_OVERLOAD_CAST) 15 template <
typename... Args>
28 std::string
toString()
const {
return "ExampleMandA[value=" + std::to_string(
value) +
"]"; }
88 int get()
const {
return value; }
89 void set(
int v) { value =
v; }
108 const UserType &
get1()
const {
return v1; }
109 const UserType &
get2()
const {
return v2; }
124 int none3(std::shared_ptr<NoneTester> &obj) {
return obj ? obj->answer : -1; }
125 int none4(std::shared_ptr<NoneTester> *obj) {
return obj && *obj ? (*obj)->answer : -1; }
126 int none5(
const std::shared_ptr<NoneTester> &obj) {
return obj ? obj->answer : -1; }
156 double ro_value = 1.25;
160 using UnregisteredBase::UnregisteredBase;
161 double sum()
const {
return rw_value + ro_value; }
182 py::class_<ExampleMandA> emna(
m,
"ExampleMandA");
183 emna.def(py::init<>())
184 .def(py::init<int>())
185 .def(py::init<std::string &&>())
186 .def(py::init<const ExampleMandA &>())
208 #if defined(PYBIND11_OVERLOAD_CAST) 217 .def(
"overloaded_const",
219 .def(
"overloaded_const",
221 .def(
"overloaded_const",
223 .def(
"overloaded_const",
242 .def_static(
"add_mixed_overloads1",
244 auto emna = py::reinterpret_borrow<py::class_<ExampleMandA>>(
245 py::module_::import(
"pybind11_tests.methods_and_attributes")
246 .attr(
"ExampleMandA"));
247 emna.def(
"overload_mixed1",
254 .def_static(
"add_mixed_overloads2",
256 auto emna = py::reinterpret_borrow<py::class_<ExampleMandA>>(
257 py::module_::import(
"pybind11_tests.methods_and_attributes")
258 .attr(
"ExampleMandA"));
259 emna.def_static(
"overload_mixed2",
261 .def(
"overload_mixed2",
270 emna.attr(
"add2b") = emna.attr(
"add2");
273 py::class_<TestProperties>(
m,
"TestProperties")
281 .def_property(
"def_property_impossible",
nullptr,
nullptr)
284 .def_property_static(
"def_writeonly_static",
287 .def_property_readonly_static(
288 "def_property_readonly_static",
290 .def_property_static(
291 "def_property_writeonly_static",
294 .def_property_static(
295 "def_property_static",
298 .def_property_static(
300 [](py::object cls) {
return cls; },
301 [](
const py::object &cls,
const py::function &
f) {
f(cls); });
303 py::class_<TestPropertiesOverride, TestProperties>(
m,
"TestPropertiesOverride")
308 auto static_get1 = [](
const py::object &) ->
const UserType & {
return TestPropRVP::sv1; };
309 auto static_get2 = [](
const py::object &) ->
const UserType & {
return TestPropRVP::sv2; };
315 py::class_<TestPropRVP>(
m,
"TestPropRVP")
319 .def_property_readonly(
"ro_func", py::cpp_function(&
TestPropRVP::get2, rvp_copy))
324 .def_property_readonly_static(
"static_ro_ref", static_get1)
325 .def_property_readonly_static(
"static_ro_copy", static_get2, rvp_copy)
326 .def_property_readonly_static(
"static_ro_func", py::cpp_function(static_get2, rvp_copy))
327 .def_property_static(
"static_rw_ref", static_get1, static_set1)
328 .def_property_static(
"static_rw_copy", static_get2, static_set2, rvp_copy)
329 .def_property_static(
330 "static_rw_func", py::cpp_function(static_get2, rvp_copy), static_set2)
333 .def_property_readonly_static(
"static_rvalue",
334 [](
const py::object &) {
return UserType(1); });
337 struct MetaclassOverride {};
338 py::class_<MetaclassOverride>(
m,
"MetaclassOverride", py::metaclass((PyObject *) &PyType_Type))
339 .def_property_readonly_static(
"readonly", [](
const py::object &) {
return 1; });
342 m.def(
"overload_order", [](
const std::string &) {
return 1; });
343 m.def(
"overload_order", [](
const std::string &) {
return 2; });
344 m.def(
"overload_order", [](
int) {
return 3; });
346 "overload_order", [](
int) {
return 4; }, py::prepend{});
348 #if !defined(PYPY_VERSION) 353 DynamicClass(
const DynamicClass &) =
delete;
356 py::class_<DynamicClass>(
m,
"DynamicClass", py::dynamic_attr()).def(
py::init());
358 class CppDerivedDynamicClass :
public DynamicClass {};
359 py::class_<CppDerivedDynamicClass, DynamicClass>(
m,
"CppDerivedDynamicClass").def(
py::init());
364 #if defined(PYBIND11_DETAILED_ERROR_MESSAGES) 365 m.attr(
"detailed_error_messages_enabled") =
true;
367 m.attr(
"detailed_error_messages_enabled") =
false;
369 m.def(
"bad_arg_def_named", [] {
370 auto m = py::module_::import(
"pybind11_tests");
377 m.def(
"bad_arg_def_unnamed", [] {
378 auto m = py::module_::import(
"pybind11_tests");
389 py::class_<NoneTester, std::shared_ptr<NoneTester>>(
m,
"NoneTester").def(py::init<>());
395 m.def(
"ok_none1", &
none1);
397 m.def(
"ok_none3", &
none3);
399 m.def(
"ok_none5", &
none5);
401 m.def(
"no_none_kwarg", &
none2,
"a"_a.none(
false));
402 m.def(
"no_none_kwarg_kw_only", &
none2, py::kw_only(),
"a"_a.none(
false));
406 py::class_<NoneCastTester>(
m,
"NoneCastTester")
408 .def(py::init<int>())
410 py::implicitly_convertible<py::none, NoneCastTester>();
415 py::class_<StrIssue>(
m,
"StrIssue")
416 .def(py::init<int>())
419 [](
const StrIssue &si) {
return "StrIssue[" + std::to_string(si.
val) +
"]"; });
427 py::class_<RegisteredDerived>(
m,
"RegisteredDerived")
447 py::class_<RefQualified>(
m,
"RefQualified")
453 py::class_<RValueRefParam>(
m,
"RValueRefParam")
std::size_t func4(std::string &&s) const &
py::str overloaded(float, int)
void refQualified(int other) &
const UserType & get2() const
void print_destroyed(T *inst, Values &&...values)
std::size_t func1(std::string &&s)
std::string toString() const
const ExampleMandA & self3() const
void consume_str(std::string &&)
int constRefQualified(int other) const &
void print_copy_assigned(T *inst, Values &&...values)
void add10(const int *other)
void print_copy_created(T *inst, Values &&...values)
py::str overloaded(int, int)
void add4(ExampleMandA *other)
py::str overloaded(float, float)
Dummy type which is not exported anywhere – something to trigger a conversion error.
static constexpr auto const_
void foo(CV_QUALIFIER Matrix3d &m)
static void static_set(int v)
void print_default_created(T *inst, Values &&...values)
const ExampleMandA * self5() const
const int & internal3() const
int none5(const std::shared_ptr< NoneTester > &obj)
void add8(const int &other)
void print_move_assigned(T *inst, Values &&...values)
void operator=(ExampleMandA &&e) noexcept
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const ArgReturnType arg() const
Array< int, Dynamic, 1 > v
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
py::str overloaded(float, float) const
Array< double, 1, 3 > e(1./3., 0.5, 2.)
static py::str overloaded(float)
const UserType & get1() const
ExampleMandA(const ExampleMandA &e)
int none4(std::shared_ptr< NoneTester > *obj)
py::str overloaded(int, float) const
py::str overloaded(float, int) const
int none3(std::shared_ptr< NoneTester > &obj)
double get_double() const
std::size_t func3(std::string &&s) &
TEST_SUBMODULE(methods_and_attributes, m)
pybind11::detail::overload_cast_impl< Args... > overload_cast_
void operator=(const ExampleMandA &e)
UserType get_rvalue() const
py::str overloaded(int, int) const
void print_created(T *inst, Values &&...values)
detail::initimpl::constructor< Args... > init()
Binds an existing constructor taking arguments Args...
std::size_t func2(std::string &&s) const
void add2(ExampleMandA &other)
int none2(NoneTester *obj)
int EIGEN_BLAS_FUNC() copy(int *n, RealScalar *px, int *incx, RealScalar *py, int *incy)
int none1(const NoneTester &obj)
void add3(const ExampleMandA &other)
ExampleMandA(ExampleMandA &&e) noexcept
void print_move_created(T *inst, Values &&...values)
py::str overloaded(int, float)
void add1(ExampleMandA other)
py::str overloaded(int) const
ExampleMandA(std::string &&)
void add5(const ExampleMandA *other)