File tree 4 files changed +80
-1
lines changed
4 files changed +80
-1
lines changed Original file line number Diff line number Diff line change @@ -151,7 +151,7 @@ function(pybind11_enable_warnings target_name)
151
151
endif ()
152
152
endfunction ()
153
153
154
- set (test_targets pybind11_tests)
154
+ set (test_targets pybind11_tests test_move_arg )
155
155
156
156
# Build pybind11_cross_module_tests if any test_whatever.py are being built that require it
157
157
foreach (t ${PYBIND11_CROSS_MODULE_TESTS} )
Original file line number Diff line number Diff line change
1
+ #include " test_move_arg.h"
2
+ #include < pybind11/pybind11.h>
3
+ #include < pybind11/iostream.h>
4
+ #include < sstream>
5
+
6
+ namespace py = pybind11;
7
+
8
+ PYBIND11_MODULE (test_move_arg, m) {
9
+ py::class_<Item>(m, " Item" )
10
+ .def (py::init<int >(), py::call_guard<py::scoped_ostream_redirect>())
11
+ .def (" __repr__" , [](const Item& item) {
12
+ std::stringstream ss;
13
+ ss << " py " << item;
14
+ return ss.str ();
15
+ }, py::call_guard<py::scoped_ostream_redirect>());
16
+
17
+ m.def (" access" , [](const Item& item) {
18
+ std::cout << " access " << item << " \n " ;
19
+ }, py::call_guard<py::scoped_ostream_redirect>());
20
+
21
+ #if 0 // rvalue arguments fail during compilation
22
+ m.def("consume", [](Item&& item) {
23
+ std::cout << "consume " << item << "\n ";
24
+ Item sink(std::move(item));
25
+ std::cout << " old: " << item << "\n new: " << sink << "\n";
26
+ }, py::call_guard<py::scoped_ostream_redirect>());
27
+ #endif
28
+
29
+ m.def (" working" , [](int && a) { std::cout << a << " \n " ; },
30
+ py::call_guard<py::scoped_ostream_redirect>());
31
+ }
Original file line number Diff line number Diff line change
1
+ #pragma once
2
+ #include < memory>
3
+ #include < vector>
4
+ #include < iostream>
5
+
6
+ class Item ;
7
+ std::ostream& operator <<(std::ostream& os, const Item& item);
8
+
9
+ /* * Item class requires unique instances, i.e. only supports move construction */
10
+ class Item
11
+ {
12
+ public:
13
+ Item (int value) : value_(std::make_unique<int >(value)) {
14
+ std::cout<< " new " << *this << " \n " ;
15
+ }
16
+ ~Item () {
17
+ std::cout << " destroy " << *this << " \n " ;
18
+ }
19
+ Item (const Item&) = delete ;
20
+ Item (Item&& other) {
21
+ std::cout << " move " << other << " -> " ;
22
+ value_ = std::move (other.value_ );
23
+ std::cout << *this << " \n " ;
24
+ }
25
+
26
+ std::unique_ptr<int > value_;
27
+ };
28
+
29
+ std::ostream& operator <<(std::ostream& os, const Item& item) {
30
+ os << " item " << &item << " (" ;
31
+ if (item.value_ ) os << *item.value_ ;
32
+ os << " )" ;
33
+ return os;
34
+ }
Original file line number Diff line number Diff line change
1
+ import pytest
2
+ from test_move_arg import Item , access
3
+
4
+
5
+ def test ():
6
+ item = Item (42 )
7
+ other = item
8
+ access (item )
9
+ print (item )
10
+ del item
11
+ print (other )
12
+
13
+ if __name__ == "__main__" :
14
+ test ()
You can’t perform that action at this time.
0 commit comments