10 #if defined(__INTEL_COMPILER) && __cplusplus >= 201703L
14 # include <aligned_new>
38 namespace pr4220_tripped_over_this {
45 return "This is really only meant to exercise successful compilation.";
51 py::class_<Empty0>(
m,
"Empty0").def(py::init<>()).def(
"get_msg", get_msg<Empty0>);
61 struct NoConstructor {
62 NoConstructor() =
default;
63 NoConstructor(
const NoConstructor &) =
default;
64 NoConstructor(NoConstructor &&) =
default;
65 static NoConstructor *new_instance() {
66 auto *ptr =
new NoConstructor();
72 struct NoConstructorNew {
73 NoConstructorNew() =
default;
74 NoConstructorNew(
const NoConstructorNew &) =
default;
75 NoConstructorNew(NoConstructorNew &&) =
default;
76 static NoConstructorNew *new_instance() {
77 auto *ptr =
new NoConstructorNew();
84 py::class_<NoConstructor>(
m,
"NoConstructor")
85 .def_static(
"new_instance", &NoConstructor::new_instance,
"Return an instance");
87 py::class_<NoConstructorNew>(
m,
"NoConstructorNew")
88 .def(
py::init([]() {
return nullptr; }))
89 .def_static(
"__new__",
90 [](
const py::object &) {
return NoConstructorNew::new_instance(); });
95 Pet(
const std::string &
name,
const std::string &species)
96 : m_name(
name), m_species(species) {}
97 std::string
name()
const {
return m_name; }
98 std::string species()
const {
return m_species; }
102 std::string m_species;
105 class Dog :
public Pet {
107 explicit Dog(
const std::string &
name) : Pet(
name,
"dog") {}
108 std::string
bark()
const {
return "Woof!"; }
111 class Rabbit :
public Pet {
113 explicit Rabbit(
const std::string &
name) : Pet(
name,
"parrot") {}
116 class Hamster :
public Pet {
118 explicit Hamster(
const std::string &
name) : Pet(
name,
"rodent") {}
121 class Chimera :
public Pet {
122 Chimera() : Pet(
"Kimmy",
"chimera") {}
125 py::class_<Pet> pet_class(
m,
"Pet");
126 pet_class.def(py::init<std::string, std::string>())
128 .def(
"species", &Pet::species);
131 py::class_<Dog>(
m,
"Dog", pet_class).def(py::init<std::string>());
134 py::class_<Rabbit, Pet>(
m,
"Rabbit").def(py::init<std::string>());
137 py::class_<Hamster, Pet>(
m,
"Hamster").def(py::init<std::string>());
140 py::class_<Chimera, Pet>(
m,
"Chimera");
142 m.def(
"pet_name_species",
143 [](
const Pet &pet) {
return pet.name() +
" is a " + pet.species(); });
144 m.def(
"dog_bark", [](
const Dog &dog) {
return dog.
bark(); });
148 BaseClass() =
default;
149 BaseClass(
const BaseClass &) =
default;
150 BaseClass(BaseClass &&) =
default;
151 virtual ~BaseClass() =
default;
153 struct DerivedClass1 : BaseClass {};
154 struct DerivedClass2 : BaseClass {};
156 py::class_<BaseClass>(
m,
"BaseClass").def(py::init<>());
157 py::class_<DerivedClass1>(
m,
"DerivedClass1").def(py::init<>());
158 py::class_<DerivedClass2>(
m,
"DerivedClass2").def(py::init<>());
160 m.def(
"return_class_1", []() -> BaseClass * {
return new DerivedClass1(); });
161 m.def(
"return_class_2", []() -> BaseClass * {
return new DerivedClass2(); });
162 m.def(
"return_class_n", [](
int n) -> BaseClass * {
164 return new DerivedClass1();
167 return new DerivedClass2();
169 return new BaseClass();
171 m.def(
"return_none", []() -> BaseClass * {
return nullptr; });
174 m.def(
"check_instances", [](
const py::list &
l) {
176 py::isinstance<py::dict>(
l[1]),
177 py::isinstance<Pet>(
l[2]),
178 py::isinstance<Pet>(
l[3]),
179 py::isinstance<Dog>(
l[4]),
180 py::isinstance<Rabbit>(
l[5]),
181 py::isinstance<UnregisteredType>(
l[6]));
187 m.def(
"check_type", [](
int category) {
193 return py::type::of<DerivedClass1>();
195 return py::type::of<Invalid>();
198 m.def(
"get_type_of", [](py::object ob) {
return py::type::of(std::move(ob)); });
200 m.def(
"get_type_classic", [](py::handle
h) {
return h.get_type(); });
202 m.def(
"as_type", [](
const py::object &ob) {
return py::type(ob); });
205 struct MismatchBase1 {};
206 struct MismatchDerived1 : MismatchBase1 {};
208 struct MismatchBase2 {};
209 struct MismatchDerived2 : MismatchBase2 {};
211 m.def(
"mismatched_holder_1", []() {
212 auto mod = py::module_::import(
"__main__");
213 py::class_<MismatchBase1, std::shared_ptr<MismatchBase1>>(mod,
"MismatchBase1");
214 py::class_<MismatchDerived1, MismatchBase1>(mod,
"MismatchDerived1");
216 m.def(
"mismatched_holder_2", []() {
217 auto mod = py::module_::import(
"__main__");
218 py::class_<MismatchBase2>(mod,
"MismatchBase2");
219 py::class_<MismatchDerived2, std::shared_ptr<MismatchDerived2>, MismatchBase2>(
220 mod,
"MismatchDerived2");
226 static std::unique_ptr<MyBase>
make() {
return std::unique_ptr<MyBase>(
new MyBase()); }
229 struct MyDerived : MyBase {
230 static std::unique_ptr<MyDerived>
make() {
231 return std::unique_ptr<MyDerived>(
new MyDerived());
235 py::class_<MyBase>(
m,
"MyBase").def_static(
"make", &
MyBase::make);
237 py::class_<MyDerived, MyBase>(
m,
"MyDerived")
242 struct ConvertibleFromUserType {
245 explicit ConvertibleFromUserType(UserType u) :
i(u.value()) {}
248 py::class_<ConvertibleFromUserType>(
m,
"AcceptsUserType").def(py::init<UserType>());
249 py::implicitly_convertible<UserType, ConvertibleFromUserType>();
251 m.def(
"implicitly_convert_argument", [](
const ConvertibleFromUserType &r) {
return r.i; });
252 m.def(
"implicitly_convert_variable", [](
const py::object &o) {
256 const auto &r = o.cast<
const ConvertibleFromUserType &>();
259 m.add_object(
"implicitly_convert_variable_fail", [&] {
260 auto f = [](PyObject *, PyObject *
args) -> PyObject * {
261 auto o = py::reinterpret_borrow<py::tuple>(
args)[0];
263 o.cast<
const ConvertibleFromUserType &>();
264 }
catch (
const py::cast_error &
e) {
265 return py::str(
e.what()).release().ptr();
267 return py::str().release().ptr();
270 auto *def =
new PyMethodDef{
"f",
f, METH_VARARGS,
nullptr};
271 py::capsule def_capsule(def,
272 [](
void *ptr) {
delete reinterpret_cast<PyMethodDef *
>(ptr); });
273 return py::reinterpret_steal<py::object>(
274 PyCFunction_NewEx(def, def_capsule.ptr(),
m.ptr()));
280 static void *
operator new(
size_t s) {
282 return ::operator
new(
s);
284 static void *
operator new(
size_t s,
void *ptr) {
288 static void operator delete(
void *
p) {
290 return ::operator
delete(
p);
293 struct HasOpNewDelSize {
295 static void *
operator new(
size_t s) {
297 return ::operator
new(
s);
299 static void *
operator new(
size_t s,
void *ptr) {
303 static void operator delete(
void *
p,
size_t s) {
305 return ::operator
delete(
p);
308 struct AliasedHasOpNewDelSize {
310 static void *
operator new(
size_t s) {
312 return ::operator
new(
s);
314 static void *
operator new(
size_t s,
void *ptr) {
318 static void operator delete(
void *
p,
size_t s) {
320 return ::operator
delete(
p);
322 virtual ~AliasedHasOpNewDelSize() =
default;
323 AliasedHasOpNewDelSize() =
default;
324 AliasedHasOpNewDelSize(
const AliasedHasOpNewDelSize &) =
delete;
326 struct PyAliasedHasOpNewDelSize : AliasedHasOpNewDelSize {
327 PyAliasedHasOpNewDelSize() =
default;
328 explicit PyAliasedHasOpNewDelSize(
int) {}
331 struct HasOpNewDelBoth {
333 static void *
operator new(
size_t s) {
335 return ::operator
new(
s);
337 static void *
operator new(
size_t s,
void *ptr) {
341 static void operator delete(
void *
p) {
343 return ::operator
delete(
p);
345 static void operator delete(
void *
p,
size_t s) {
347 return ::operator
delete(
p);
350 py::class_<HasOpNewDel>(
m,
"HasOpNewDel").def(py::init<>());
351 py::class_<HasOpNewDelSize>(
m,
"HasOpNewDelSize").def(py::init<>());
352 py::class_<HasOpNewDelBoth>(
m,
"HasOpNewDelBoth").def(py::init<>());
353 py::class_<AliasedHasOpNewDelSize, PyAliasedHasOpNewDelSize> aliased(
m,
354 "AliasedHasOpNewDelSize");
355 aliased.def(py::init<>());
356 aliased.attr(
"size_noalias") = py::int_(
sizeof(AliasedHasOpNewDelSize));
357 aliased.attr(
"size_alias") = py::int_(
sizeof(PyAliasedHasOpNewDelSize));
361 bind_local<LocalExternal, 17>(
m,
"LocalExternal", py::module_local());
372 class PublicistA :
public ProtectedA {
377 py::class_<ProtectedA>(
m,
"ProtectedA").def(py::init<>()).def(
"foo", &
PublicistA::foo);
381 virtual ~ProtectedB() =
default;
382 ProtectedB() =
default;
383 ProtectedB(
const ProtectedB &) =
delete;
386 virtual int foo()
const {
return value; }
387 virtual void *void_foo() {
return static_cast<void *
>(&
value); }
388 virtual void *get_self() {
return static_cast<void *
>(
this); }
394 class TrampolineB :
public ProtectedB {
401 class PublicistB :
public ProtectedB {
406 ~PublicistB()
override {};
408 using ProtectedB::get_self;
409 using ProtectedB::void_foo;
412 m.def(
"read_foo", [](
const void *original) {
413 const int *ptr =
reinterpret_cast<const int *
>(original);
417 m.def(
"pointers_equal",
418 [](
const void *original,
const void *comparison) {
return original == comparison; });
420 py::class_<ProtectedB, TrampolineB>(
m,
"ProtectedB")
423 .def(
"void_foo", &PublicistB::void_foo)
424 .def(
"get_self", &PublicistB::get_self);
427 struct BraceInitialization {
432 py::class_<BraceInitialization>(
m,
"BraceInitialization")
433 .def(py::init<int, const std::string &>())
434 .def_readwrite(
"field1", &BraceInitialization::field1)
435 .def_readwrite(
"field2", &BraceInitialization::field2);
439 py::class_<NoBraceInitialization>(
m,
"NoBraceInitialization")
445 struct BogusImplicitConversion {
446 BogusImplicitConversion(
const BogusImplicitConversion &) =
default;
449 py::class_<BogusImplicitConversion>(
m,
"BogusImplicitConversion")
450 .def(py::init<const BogusImplicitConversion &>());
452 py::implicitly_convertible<int, BogusImplicitConversion>();
459 py::class_<NestBase>
base(
m,
"NestBase");
460 base.def(py::init<>());
461 py::class_<Nested>(
base,
"Nested")
463 .def(
"fn", [](Nested &,
int, NestBase &, Nested &) {})
464 .def(
"fa", [](Nested &,
int, NestBase &, Nested &) {},
"a"_a,
"b"_a,
"c"_a);
465 base.def(
"g", [](NestBase &, Nested &) {});
466 base.def(
"h", []() {
return NestBase(); });
473 struct NotRegistered {};
474 struct StringWrapper {
477 m.def(
"test_error_after_conversions", [](
int) {});
478 m.def(
"test_error_after_conversions",
479 [](
const StringWrapper &) -> NotRegistered {
return {}; });
480 py::class_<StringWrapper>(
m,
"StringWrapper").def(py::init<std::string>());
481 py::implicitly_convertible<std::string, StringWrapper>();
483 #if defined(PYBIND11_CPP17)
487 py::class_<Aligned>(
m,
"Aligned").def(py::init<>()).def(
"ptr", &Aligned::ptr);
491 struct IsFinal final {};
492 py::class_<IsFinal>(
m,
"IsFinal", py::is_final());
495 struct IsNonFinalFinal {};
496 py::class_<IsNonFinalFinal>(
m,
"IsNonFinalFinal", py::is_final());
499 struct PyPrintDestructor {
500 PyPrintDestructor() =
default;
501 ~PyPrintDestructor() {
py::print(
"Print from destructor"); }
502 void throw_something() {
throw std::runtime_error(
"error"); }
504 py::class_<PyPrintDestructor>(
m,
"PyPrintDestructor")
506 .def(
"throw_something", &PyPrintDestructor::throw_something);
509 struct SamePointer {};
510 static SamePointer samePointer;
511 py::class_<SamePointer, std::unique_ptr<SamePointer, py::nodelete>>(
m,
"SamePointer")
512 .def(
py::init([]() {
return &samePointer; }));
515 py::class_<Empty>(
m,
"Empty").def(py::init<>());
518 struct BaseWithNested {
522 struct DerivedWithNested : BaseWithNested {
526 py::class_<BaseWithNested> baseWithNested_class(
m,
"BaseWithNested");
527 py::class_<DerivedWithNested, BaseWithNested> derivedWithNested_class(
m,
"DerivedWithNested");
528 py::class_<BaseWithNested::Nested>(baseWithNested_class,
"Nested")
529 .def_static(
"get_name", []() {
return "BaseWithNested::Nested"; });
530 py::class_<DerivedWithNested::Nested>(derivedWithNested_class,
"Nested")
531 .def_static(
"get_name", []() {
return "DerivedWithNested::Nested"; });
535 struct OtherDuplicate {};
536 struct DuplicateNested {};
537 struct OtherDuplicateNested {};
539 m.def(
"register_duplicate_class_name", [](
const py::module_ &
m) {
540 py::class_<Duplicate>(
m,
"Duplicate");
541 py::class_<OtherDuplicate>(
m,
"Duplicate");
543 m.def(
"register_duplicate_class_type", [](
const py::module_ &
m) {
544 py::class_<OtherDuplicate>(
m,
"OtherDuplicate");
545 py::class_<OtherDuplicate>(
m,
"YetAnotherDuplicate");
547 m.def(
"register_duplicate_nested_class_name", [](
const py::object >) {
548 py::class_<DuplicateNested>(gt,
"DuplicateNested");
549 py::class_<OtherDuplicateNested>(gt,
"DuplicateNested");
551 m.def(
"register_duplicate_nested_class_type", [](
const py::object >) {
552 py::class_<OtherDuplicateNested>(gt,
"OtherDuplicateNested");
553 py::class_<OtherDuplicateNested>(gt,
"YetAnotherDuplicateNested");
571 using DoesntBreak3 = py::class_<BreaksBase<3>, std::unique_ptr<BreaksBase<3>>>;
576 using DoesntBreak8 = py::class_<BreaksBase<8>, std::shared_ptr<BreaksBase<8>>>;
577 #define CHECK_BASE(N) \
578 static_assert(std::is_same<typename DoesntBreak##N::type, BreaksBase<(N)>>::value, \
579 "DoesntBreak" #N " has wrong type!")
588 #define CHECK_ALIAS(N) \
590 DoesntBreak##N::has_alias \
591 && std::is_same<typename DoesntBreak##N::type_alias, BreaksTramp<(N)>>::value, \
592 "DoesntBreak" #N " has wrong type_alias!")
593 #define CHECK_NOALIAS(N) \
594 static_assert(!DoesntBreak##N::has_alias \
595 && std::is_void<typename DoesntBreak##N::type_alias>::value, \
596 "DoesntBreak" #N " has type alias, but shouldn't!")
605 #define CHECK_HOLDER(N, TYPE) \
606 static_assert(std::is_same<typename DoesntBreak##N::holder_type, \
607 std::TYPE##_ptr<BreaksBase<(N)>>>::value, \
608 "DoesntBreak" #N " has wrong holder_type!")
623 #define CHECK_BROKEN(N) \
624 static_assert(std::is_same<typename Breaks##N::type, BreaksBase<-(N)>>::value, \
625 "Breaks1 has wrong type!");
627 #ifdef PYBIND11_NEVER_DEFINED_EVER