16 # pragma warning(disable: 4324) // warning C4324: structure was padded due to alignment specifier 30 struct NoConstructor {
31 NoConstructor() =
default;
32 NoConstructor(
const NoConstructor &) =
default;
33 NoConstructor(NoConstructor &&) =
default;
34 static NoConstructor *new_instance() {
35 auto *ptr =
new NoConstructor();
43 .def_static(
"new_instance", &NoConstructor::new_instance,
"Return an instance");
48 Pet(
const std::string &name,
const std::string &species)
49 : m_name(name), m_species(species) {}
50 std::string name()
const {
return m_name; }
51 std::string species()
const {
return m_species; }
54 std::string m_species;
57 class Dog :
public Pet {
59 Dog(
const std::string &name) : Pet(name,
"dog") {}
60 std::string bark()
const {
return "Woof!"; }
63 class Rabbit :
public Pet {
65 Rabbit(
const std::string &name) : Pet(name,
"parrot") {}
68 class Hamster :
public Pet {
70 Hamster(
const std::string &name) : Pet(name,
"rodent") {}
73 class Chimera :
public Pet {
74 Chimera() : Pet(
"Kimmy",
"chimera") {}
79 .
def(py::init<std::string, std::string>())
80 .def(
"name", &Pet::name)
81 .def(
"species", &Pet::species);
85 .def(py::init<std::string>());
89 .def(py::init<std::string>());
93 .def(py::init<std::string>());
98 m.
def(
"pet_name_species", [](
const Pet &pet) {
return pet.name() +
" is a " + pet.species(); });
99 m.
def(
"dog_bark", [](
const Dog &dog) {
return dog.
bark(); });
103 BaseClass() =
default;
104 BaseClass(
const BaseClass &) =
default;
105 BaseClass(BaseClass &&) =
default;
106 virtual ~BaseClass() {}
108 struct DerivedClass1 : BaseClass { };
109 struct DerivedClass2 : BaseClass { };
115 m.
def(
"return_class_1", []() -> BaseClass* {
return new DerivedClass1(); });
116 m.
def(
"return_class_2", []() -> BaseClass* {
return new DerivedClass2(); });
117 m.
def(
"return_class_n", [](
int n) -> BaseClass* {
118 if (n == 1)
return new DerivedClass1();
119 if (n == 2)
return new DerivedClass2();
120 return new BaseClass();
122 m.
def(
"return_none", []() -> BaseClass* {
return nullptr; });
127 py::isinstance<py::tuple>(l[0]),
128 py::isinstance<py::dict>(l[1]),
129 py::isinstance<Pet>(l[2]),
130 py::isinstance<Pet>(l[3]),
131 py::isinstance<Dog>(l[4]),
132 py::isinstance<Rabbit>(l[5]),
133 py::isinstance<UnregisteredType>(l[6])
138 struct MismatchBase1 { };
139 struct MismatchDerived1 : MismatchBase1 { };
141 struct MismatchBase2 { };
142 struct MismatchDerived2 : MismatchBase2 { };
144 m.
def(
"mismatched_holder_1", []() {
145 auto mod = py::module::import(
"__main__");
149 m.
def(
"mismatched_holder_2", []() {
150 auto mod = py::module::import(
"__main__");
153 MismatchBase2>(mod,
"MismatchDerived2");
159 static std::unique_ptr<MyBase> make() {
160 return std::unique_ptr<MyBase>(
new MyBase());
164 struct MyDerived : MyBase {
165 static std::unique_ptr<MyDerived> make() {
166 return std::unique_ptr<MyDerived>(
new MyDerived());
171 .def_static(
"make", &MyBase::make);
174 .def_static(
"make", &MyDerived::make)
178 struct ConvertibleFromUserType {
181 ConvertibleFromUserType(UserType u) :
i(u.value()) { }
185 .def(py::init<UserType>());
186 py::implicitly_convertible<UserType, ConvertibleFromUserType>();
188 m.
def(
"implicitly_convert_argument", [](
const ConvertibleFromUserType &r) {
return r.i; });
193 const auto &r = o.
cast<
const ConvertibleFromUserType &>();
196 m.add_object(
"implicitly_convert_variable_fail", [&] {
197 auto f = [](PyObject *, PyObject *args) -> PyObject * {
198 auto o = py::reinterpret_borrow<py::tuple>(args)[0];
200 o.
cast<
const ConvertibleFromUserType &>();
204 return py::str().release().ptr();
207 auto def =
new PyMethodDef{
"f", f, METH_VARARGS,
nullptr};
208 return py::reinterpret_steal<py::object>(PyCFunction_NewEx(def,
nullptr, m.
ptr()));
214 static void *
operator new(
size_t s) {
py::print(
"A new", s); return ::operator
new(s); }
215 static void *
operator new(
size_t s,
void *ptr) {
py::print(
"A placement-new", s);
return ptr; }
216 static void operator delete(
void *p) {
py::print(
"A delete"); return ::operator
delete(p); }
218 struct HasOpNewDelSize {
220 static void *
operator new(
size_t s) {
py::print(
"B new", s); return ::operator
new(s); }
221 static void *
operator new(
size_t s,
void *ptr) {
py::print(
"B placement-new", s);
return ptr; }
222 static void operator delete(
void *p,
size_t s) {
py::print(
"B delete", s); return ::operator
delete(p); }
224 struct AliasedHasOpNewDelSize {
226 static void *
operator new(
size_t s) {
py::print(
"C new", s); return ::operator
new(s); }
227 static void *
operator new(
size_t s,
void *ptr) {
py::print(
"C placement-new", s);
return ptr; }
228 static void operator delete(
void *p,
size_t s) {
py::print(
"C delete", s); return ::operator
delete(p); }
229 virtual ~AliasedHasOpNewDelSize() =
default;
231 struct PyAliasedHasOpNewDelSize : AliasedHasOpNewDelSize {
232 PyAliasedHasOpNewDelSize() =
default;
233 PyAliasedHasOpNewDelSize(
int) { }
236 struct HasOpNewDelBoth {
238 static void *
operator new(
size_t s) {
py::print(
"D new", s); return ::operator
new(s); }
239 static void *
operator new(
size_t s,
void *ptr) {
py::print(
"D placement-new", s);
return ptr; }
240 static void operator delete(
void *p) {
py::print(
"D delete"); return ::operator
delete(p); }
241 static void operator delete(
void *p,
size_t s) {
py::print(
"D wrong delete", s); return ::operator
delete(p); }
247 aliased.
def(py::init<>());
248 aliased.
attr(
"size_noalias") =
py::int_(
sizeof(AliasedHasOpNewDelSize));
249 aliased.
attr(
"size_alias") =
py::int_(
sizeof(PyAliasedHasOpNewDelSize));
258 int foo()
const {
return value; }
264 class PublicistA :
public ProtectedA {
266 using ProtectedA::foo;
271 #
if !defined(_MSC_VER) || _MSC_VER >= 1910
272 .
def(
"foo", &PublicistA::foo);
274 .
def(
"foo",
static_cast<int (ProtectedA::*)() const
>(&PublicistA::foo));
279 virtual ~ProtectedB() =
default;
282 virtual int foo()
const {
return value; }
288 class TrampolineB :
public ProtectedB {
293 class PublicistB :
public ProtectedB {
295 using ProtectedB::foo;
300 #
if !defined(_MSC_VER) || _MSC_VER >= 1910
301 .
def(
"foo", &PublicistB::foo);
303 .
def(
"foo",
static_cast<int (ProtectedB::*)() const
>(&PublicistB::foo));
307 struct BraceInitialization {
313 .def(py::init<int, const std::string &>())
314 .def_readwrite(
"field1", &BraceInitialization::field1)
325 struct BogusImplicitConversion {
326 BogusImplicitConversion(
const BogusImplicitConversion &) { }
330 .def(py::init<const BogusImplicitConversion &>());
332 py::implicitly_convertible<int, BogusImplicitConversion>();
340 base.
def(py::init<>());
343 .def(
"fn", [](Nested &,
int, NestBase &, Nested &) {})
344 .def(
"fa", [](Nested &,
int, NestBase &, Nested &) {},
345 "a"_a,
"b"_a,
"c"_a);
346 base.
def(
"g", [](NestBase &, Nested &) {});
347 base.
def(
"h", []() {
return NestBase(); });
354 struct NotRegistered {};
355 struct StringWrapper { std::string
str; };
356 m.
def(
"test_error_after_conversions", [](
int) {});
357 m.
def(
"test_error_after_conversions",
358 [](StringWrapper) -> NotRegistered {
return {}; });
360 py::implicitly_convertible<std::string, StringWrapper>();
362 #if defined(PYBIND11_CPP17) 363 struct alignas(1024) Aligned {
364 std::uintptr_t ptr()
const {
return (uintptr_t)
this; }
368 .def(
"ptr", &Aligned::ptr);
383 #define CHECK_BASE(N) static_assert(std::is_same<typename DoesntBreak##N::type, BreaksBase<N>>::value, \ 384 "DoesntBreak" #N " has wrong type!") 386 #define CHECK_ALIAS(N) static_assert(DoesntBreak##N::has_alias && std::is_same<typename DoesntBreak##N::type_alias, BreaksTramp<N>>::value, \ 387 "DoesntBreak" #N " has wrong type_alias!") 388 #define CHECK_NOALIAS(N) static_assert(!DoesntBreak##N::has_alias && std::is_void<typename DoesntBreak##N::type_alias>::value, \ 389 "DoesntBreak" #N " has type alias, but shouldn't!") 391 #define CHECK_HOLDER(N, TYPE) static_assert(std::is_same<typename DoesntBreak##N::holder_type, std::TYPE##_ptr<BreaksBase<N>>>::value, \ 392 "DoesntBreak" #N " has wrong holder_type!") 401 #define CHECK_BROKEN(N) static_assert(std::is_same<typename Breaks##N::type, BreaksBase<-N>>::value, \ 402 "Breaks1 has wrong type!");
PyObject * ptr() const
Return the underlying PyObject * pointer.
NoBraceInitialization(std::initializer_list< T > l)
py::class_< BreaksBase< 2 >, BreaksTramp< 2 >, std::unique_ptr< BreaksBase< 2 > > > DoesntBreak2
void print_destroyed(T *inst, Values &&...values)
py::class_< BreaksBase< 4 >, BreaksTramp< 4 > > DoesntBreak4
py::class_< BreaksBase< 8 >, std::shared_ptr< BreaksBase< 8 > > > DoesntBreak8
test_initializer class_("class_", test_submodule_class_)
obj_attr_accessor attr(handle key) const
op_< op_int, op_u, self_t, undefined_t > int_(const self_t &)
py::class_< BreaksBase< 1 >, std::unique_ptr< BreaksBase< 1 > >, BreaksTramp< 1 > > DoesntBreak1
detail::initimpl::constructor< Args... > init()
Binds an existing constructor taking arguments Args...
NoBraceInitialization(std::vector< int > v)
py::class_< BreaksBase< 5 > > DoesntBreak5
class_ & def_static(const char *name_, Func &&f, const Extra &... extra)
void print_created(T *inst, Values &&...values)
py::class_< BreaksBase< 6 >, std::shared_ptr< BreaksBase< 6 > >, BreaksTramp< 6 > > DoesntBreak6
py::class_< BreaksBase< 7 >, BreaksTramp< 7 >, std::shared_ptr< BreaksBase< 7 > > > DoesntBreak7
detail::enable_if_t<!detail::move_never< T >::value, T > move(object &&obj)
#define CHECK_HOLDER(N, TYPE)
py::class_< BreaksBase< 3 >, std::unique_ptr< BreaksBase< 3 > > > DoesntBreak3
void print(Args &&...args)
#define TEST_SUBMODULE(name, variable)
bool typename Extra class_ & def(const char *name_, Func &&f, const Extra &... extra)
Annotation that marks a class as local to the module:
#define PYBIND11_OVERLOAD(ret_type, cname, fn,...)
class_ & def_readwrite(const char *name, D C::*pm, const Extra &... extra)