Skip to content

Commit fb0ecaa

Browse files
committed
attempt to allow rvalue references of custom types
1 parent 98ea003 commit fb0ecaa

File tree

3 files changed

+5
-4
lines changed

3 files changed

+5
-4
lines changed

include/pybind11/cast.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -880,10 +880,11 @@ template <typename type> class type_caster_base : public type_caster_generic {
880880
nullptr, nullptr, holder);
881881
}
882882

883-
template <typename T> using cast_op_type = detail::cast_op_type<T>;
883+
template <typename T> using cast_op_type = detail::movable_cast_op_type<T>;
884884

885885
operator itype*() { return (type *) value; }
886886
operator itype&() { if (!value) throw reference_cast_error(); return *((itype *) value); }
887+
operator itype&&() && { if (!value) throw reference_cast_error(); return std::move(*((itype *) value)); }
887888

888889
protected:
889890
using Constructor = void *(*)(const void *);

tests/test_move_arg.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ PYBIND11_MODULE(test_move_arg, m) {
1818
std::cout << "access " << item << "\n";
1919
}, py::call_guard<py::scoped_ostream_redirect>());
2020

21-
#if 0 // rvalue arguments fail during compilation
21+
#if 1 // works for this example now, but failing for other unit tests
2222
m.def("consume", [](Item&& item) {
2323
std::cout << "consume " << item << "\n ";
2424
Item sink(std::move(item));

tests/test_move_arg.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import pytest
2-
from test_move_arg import Item, access
2+
from test_move_arg import *
33

44

55
def test():
66
item = Item(42)
77
other = item
88
access(item)
9-
print(item)
9+
consume(item)
1010
del item
1111
print(other)
1212

0 commit comments

Comments
 (0)