15 template <
typename derived>
89 static constexpr
auto name =
_(
"CopyOnlyInt");
93 if (!src)
return none().release();
94 return cast(*src, policy, parent);
98 template <
typename T>
using cast_op_type = pybind11::detail::cast_op_type<T>;
105 py::class_<lacking_copy_ctor>(
m,
"lacking_copy_ctor")
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;
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) {
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);
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)
MoveOrCopyInt(MoveOrCopyInt &&m)
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)
pybind11::detail::cast_op_type< T > cast_op_type
void print_default_created(T *inst, Values &&...values)
void print_move_assigned(T *inst, Values &&...values)
EIGEN_DEVICE_FUNC NewType cast(const OldType &x)
MoveOrCopyInt & operator=(MoveOrCopyInt &&m)
static handle cast(const MoveOrCopyInt &m, return_value_policy r, handle p)
CopyOnlyInt & operator=(const CopyOnlyInt &c)
void print_created(T *inst, Values &&...values)
MoveOnlyInt(MoveOnlyInt &&m)
bool load(handle src, bool)
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
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.
void swap(mpfr::mpreal &x, mpfr::mpreal &y)
#define PYBIND11_TYPE_CASTER(type, py_name)
#define PYBIND11_NAMESPACE_END(name)
MoveOrCopyInt(const MoveOrCopyInt &c)
#define PYBIND11_NAMESPACE_BEGIN(name)
MoveOnlyInt & operator=(MoveOnlyInt &&m)