completed implicit type casters for reference_wrapper
diff --git a/docs/changelog.rst b/docs/changelog.rst
index d14dad8..80d14a8 100644
--- a/docs/changelog.rst
+++ b/docs/changelog.rst
@@ -16,6 +16,7 @@
* Added a ``get_include()`` function to the Python module that returns the path
of the directory containing the installed pybind11 header files
* Documentation improvements: import issues, symbol visibility, pickling, limitations
+* Added casting support for ``std::reference_wrapper<>``
1.4 (April 7, 2016)
--------------------------
diff --git a/example/issues.cpp b/example/issues.cpp
index e2048b1..c8af756 100644
--- a/example/issues.cpp
+++ b/example/issues.cpp
@@ -20,7 +20,7 @@
}
};
-struct Placeholder { int i; };
+struct Placeholder { int i; Placeholder(int i) : i(i) { } };
void dispatch_issue_go(const Base * b) { b->dispatch(); }
@@ -42,17 +42,19 @@
m2.def("dispatch_issue_go", &dispatch_issue_go);
py::class_<Placeholder>(m2, "Placeholder")
+ .def(py::init<int>())
.def("__repr__", [](const Placeholder &p) { return "Placeholder[" + std::to_string(p.i) + "]"; });
// #171: Can't return reference wrappers (or STL datastructures containing them)
- m2.def("return_vec_of_reference_wrapper", [] {
+ m2.def("return_vec_of_reference_wrapper", [](std::reference_wrapper<Placeholder> p4){
Placeholder *p1 = new Placeholder{1};
Placeholder *p2 = new Placeholder{2};
- Placeholder *p3 = new Placeholder{2};
+ Placeholder *p3 = new Placeholder{3};
std::vector<std::reference_wrapper<Placeholder>> v;
v.push_back(std::ref(*p1));
v.push_back(std::ref(*p2));
v.push_back(std::ref(*p3));
+ v.push_back(p4);
return v;
});
}
diff --git a/example/issues.py b/example/issues.py
index 4095daa..e0726f0 100644
--- a/example/issues.py
+++ b/example/issues.py
@@ -5,7 +5,7 @@
from example.issues import print_cchar, print_char
from example.issues import DispatchIssue, dispatch_issue_go
-from example.issues import return_vec_of_reference_wrapper
+from example.issues import Placeholder ,return_vec_of_reference_wrapper
print_cchar("const char *")
print_char('c')
@@ -28,4 +28,4 @@
b = PyClass2()
dispatch_issue_go(b)
-print(return_vec_of_reference_wrapper())
+print(return_vec_of_reference_wrapper(Placeholder(4)))
diff --git a/example/issues.ref b/example/issues.ref
index 74b34c2..fce7b95 100644
--- a/example/issues.ref
+++ b/example/issues.ref
@@ -2,4 +2,4 @@
c
Failed as expected: Tried to call pure virtual function "dispatch"
Yay..
-[Placeholder[1], Placeholder[2], Placeholder[2]]
+[Placeholder[1], Placeholder[2], Placeholder[3], Placeholder[4]]
diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h
index 1002ab1..440d9b6 100644
--- a/include/pybind11/cast.h
+++ b/include/pybind11/cast.h
@@ -246,6 +246,8 @@
static handle cast(const std::reference_wrapper<type> &src, return_value_policy policy, handle parent) {
return type_caster<type>::cast(&src.get(), policy, parent);
}
+ template <typename T> using cast_op_type = std::reference_wrapper<type>;
+ operator std::reference_wrapper<type>() { return std::ref(*((type *) this->value)); }
};
#define PYBIND11_TYPE_CASTER(type, py_name) \