15 template <
typename derived>
89 static constexpr
auto name =
_(
"CopyOnlyInt");
98 template <
typename T>
using cast_op_type = pybind11::detail::cast_op_type<T>;
105 py::class_<lacking_copy_ctor>(
m,
"lacking_copy_ctor")
107 py::return_value_policy::copy);
109 py::class_<lacking_move_ctor>(
m,
"lacking_move_ctor")
114 m.def(
"move_and_copy_casts", [](py::object o) {
116 r += py::cast<MoveOrCopyInt>(o).value;
117 r += py::cast<MoveOnlyInt>(o).value;
118 r += py::cast<CopyOnlyInt>(o).value;
119 auto m1(py::cast<MoveOrCopyInt>(o));
120 auto m2(py::cast<MoveOnlyInt>(o));
121 auto m3(py::cast<CopyOnlyInt>(o));
122 r += m1.value + m2.value + m3.value;
131 m.def(
"move_pair", [](std::pair<MoveOnlyInt, MoveOrCopyInt> p) {
132 return p.first.value + p.second.value;
134 m.def(
"move_tuple", [](std::tuple<MoveOnlyInt, MoveOrCopyInt, MoveOnlyInt> t) {
135 return std::get<0>(t).value + std::get<1>(t).value + std::get<2>(t).value;
137 m.def(
"copy_tuple", [](std::tuple<CopyOnlyInt, CopyOnlyInt> t) {
138 return std::get<0>(t).value + std::get<1>(t).value;
141 return x.first.value + std::get<0>(
x.second.first).value + std::get<1>(
x.second.first).value +
142 std::get<0>(std::get<2>(
x.second.first)).value +
x.second.second.value;
144 m.def(
"move_and_copy_cstats", []() {
147 auto &mc = ConstructorStats::get<MoveOrCopyInt>();
148 mc.move_assignments = mc.move_constructions = mc.copy_assignments = mc.copy_constructions = 0;
149 auto &mo = ConstructorStats::get<MoveOnlyInt>();
150 mo.move_assignments = mo.move_constructions = mo.copy_assignments = mo.copy_constructions = 0;
151 auto &co = ConstructorStats::get<CopyOnlyInt>();
152 co.move_assignments = co.move_constructions = co.copy_assignments = co.copy_constructions = 0;
154 d[
"MoveOrCopyInt"] =
py::cast(mc, py::return_value_policy::reference);
155 d[
"MoveOnlyInt"] =
py::cast(mo, py::return_value_policy::reference);
156 d[
"CopyOnlyInt"] =
py::cast(co, py::return_value_policy::reference);
159 #ifdef PYBIND11_HAS_OPTIONAL
161 m.attr(
"has_optional") =
true;
162 m.def(
"move_optional", [](std::optional<MoveOnlyInt> o) {
165 m.def(
"move_or_copy_optional", [](std::optional<MoveOrCopyInt> o) {
168 m.def(
"copy_optional", [](std::optional<CopyOnlyInt> o) {
171 m.def(
"move_optional_tuple", [](std::optional<std::tuple<MoveOrCopyInt, MoveOnlyInt, CopyOnlyInt>>
x) {
172 return std::get<0>(*x).value + std::get<1>(*x).value + std::get<2>(*x).value;
175 m.attr(
"has_optional") =
false;
182 struct PrivateOpNew {
185 void *
operator new(
size_t bytes) {
186 void *ptr = std::malloc(
bytes);
190 throw std::bad_alloc{};
194 m.def(
"private_op_new_value", []() {
return PrivateOpNew(); });
195 m.def(
"private_op_new_reference", []() ->
const PrivateOpNew & {
196 static PrivateOpNew
x{};
198 }, py::return_value_policy::reference);
204 MoveIssue1(
int v) : v{v} {}
205 MoveIssue1(
const MoveIssue1 &c) =
default;
206 MoveIssue1(MoveIssue1 &&) =
delete;
208 py::class_<MoveIssue1>(
m,
"MoveIssue1").def(py::init<int>()).def_readwrite(
"value", &MoveIssue1::v);
212 MoveIssue2(
int v) : v{v} {}
213 MoveIssue2(MoveIssue2 &&) =
default;
215 py::class_<MoveIssue2>(
m,
"MoveIssue2").def(py::init<int>()).def_readwrite(
"value", &MoveIssue2::v);