16 template <
typename derived>
148 return cast(*src, policy, parent);
152 template <
typename T>
160 py::class_<lacking_copy_ctor>(
m,
"lacking_copy_ctor")
163 py::class_<lacking_move_ctor>(
m,
"lacking_move_ctor")
168 m.def(
"move_and_copy_casts", [](
const py::object &o) {
170 r += py::cast<MoveOrCopyInt>(o).
value;
171 r += py::cast<MoveOnlyInt>(o).
value;
172 r += py::cast<CopyOnlyInt>(o).
value;
173 auto m1(py::cast<MoveOrCopyInt>(o));
174 auto m2(py::cast<MoveOnlyInt>(o));
175 auto m3(py::cast<CopyOnlyInt>(o));
176 r += m1.value +
m2.value + m3.value;
190 [](std::pair<MoveOnlyInt, MoveOrCopyInt>
p) {
return p.first.value + p.second.value; });
191 m.def(
"move_tuple", [](std::tuple<MoveOnlyInt, MoveOrCopyInt, MoveOnlyInt>
t) {
192 return std::get<0>(
t).
value + std::get<1>(t).value + std::get<2>(
t).
value;
194 m.def(
"copy_tuple", [](std::tuple<CopyOnlyInt, CopyOnlyInt> t) {
195 return std::get<0>(
t).
value + std::get<1>(t).value;
197 m.def(
"move_copy_nested",
201 return x.first.value + std::get<0>(
x.second.first).
value 202 + std::get<1>(
x.second.first).value
203 + std::get<0>(std::get<2>(
x.second.first)).value +
x.second.second.value;
205 m.def(
"move_and_copy_cstats", []() {
208 auto &mc = ConstructorStats::get<MoveOrCopyInt>();
209 mc.move_assignments = mc.move_constructions = mc.copy_assignments = mc.copy_constructions
211 auto &mo = ConstructorStats::get<MoveOnlyInt>();
212 mo.move_assignments = mo.move_constructions = mo.copy_assignments = mo.copy_constructions
214 auto &co = ConstructorStats::get<CopyOnlyInt>();
215 co.move_assignments = co.move_constructions = co.copy_assignments = co.copy_constructions
218 d[
"MoveOrCopyInt"] =
py::cast(mc, py::return_value_policy::reference);
219 d[
"MoveOnlyInt"] =
py::cast(mo, py::return_value_policy::reference);
220 d[
"CopyOnlyInt"] =
py::cast(co, py::return_value_policy::reference);
223 #ifdef PYBIND11_HAS_OPTIONAL 225 m.attr(
"has_optional") =
true;
226 m.def(
"move_optional", [](std::optional<MoveOnlyInt> o) {
return o->value; });
227 m.def(
"move_or_copy_optional", [](std::optional<MoveOrCopyInt> o) {
return o->value; });
228 m.def(
"copy_optional", [](std::optional<CopyOnlyInt> o) {
return o->value; });
229 m.def(
"move_optional_tuple",
230 [](std::optional<std::tuple<MoveOrCopyInt, MoveOnlyInt, CopyOnlyInt>>
x) {
231 return std::get<0>(*x).value + std::get<1>(*x).value + std::get<2>(*x).value;
234 m.attr(
"has_optional") =
false;
241 struct PrivateOpNew {
245 void *
operator new(
size_t bytes) {
246 void *ptr = std::malloc(
bytes);
250 throw std::bad_alloc{};
254 m.def(
"private_op_new_value", []() {
return PrivateOpNew(); });
256 "private_op_new_reference",
257 []() ->
const PrivateOpNew & {
258 static PrivateOpNew
x{};
261 py::return_value_policy::reference);
267 explicit MoveIssue1(
int v) : v{v} {}
268 MoveIssue1(
const MoveIssue1 &
c) =
default;
269 MoveIssue1(MoveIssue1 &&) =
delete;
271 py::class_<MoveIssue1>(
m,
"MoveIssue1")
272 .def(py::init<int>())
277 explicit MoveIssue2(
int v) : v{v} {}
278 MoveIssue2(MoveIssue2 &&) =
default;
280 py::class_<MoveIssue2>(
m,
"MoveIssue2")
281 .def(py::init<int>())
288 [](
int i) {
return std::unique_ptr<MoveIssue1>(
new MoveIssue1(i)); },
294 m.def(
"get_pytype_rvalue_castissue", [](
double i) {
return py::float_(i).cast<py::int_>(); });
TEST_SUBMODULE(copy_move_policies, m)
static handle cast(const CopyOnlyInt &m, return_value_policy r, handle p)
bool load(handle src, bool)
static const derived & get_one()
void print_destroyed(T *inst, Values &&...values)
MoveOrCopyInt & operator=(const MoveOrCopyInt &c)
void print_copy_assigned(T *inst, Values &&...values)
CopyOnlyInt(const CopyOnlyInt &c)
void print_copy_created(T *inst, Values &&...values)
static handle cast(const MoveOnlyInt &m, return_value_policy r, handle p)
bool load(handle src, bool)
detail::enable_if_t<!detail::move_never< T >::value, T > move(object &&obj)
MoveOrCopyInt(MoveOrCopyInt &&m) noexcept
pybind11::detail::cast_op_type< T > cast_op_type
void print_default_created(T *inst, Values &&...values)
MoveOrCopyInt & operator=(MoveOrCopyInt &&m) noexcept
void print_move_assigned(T *inst, Values &&...values)
Array< int, Dynamic, 1 > v
EIGEN_DEVICE_FUNC NewType cast(const OldType &x)
static handle cast(const MoveOrCopyInt &m, return_value_policy r, handle p)
void swap(GeographicLib::NearestNeighbor< dist_t, pos_t, distfun_t > &a, GeographicLib::NearestNeighbor< dist_t, pos_t, distfun_t > &b)
CopyOnlyInt & operator=(const CopyOnlyInt &c)
void print_created(T *inst, Values &&...values)
bool load(handle src, bool)
MoveOnlyInt(MoveOnlyInt &&m) noexcept
Annotation for function names.
int EIGEN_BLAS_FUNC() copy(int *n, RealScalar *px, int *incx, RealScalar *py, int *incy)
void print_move_created(T *inst, Values &&...values)
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
constexpr descr< N - 1 > const_name(char const (&text)[N])
static handle cast(const CopyOnlyInt *src, return_value_policy policy, handle parent)
return_value_policy
Approach used to cast a previously unknown C++ instance into a Python object.
#define PYBIND11_TYPE_CASTER(type, py_name)
#define PYBIND11_NAMESPACE_END(name)
MoveOnlyInt & operator=(MoveOnlyInt &&m) noexcept
MoveOrCopyInt(const MoveOrCopyInt &c)
#define PYBIND11_NAMESPACE_BEGIN(name)