1 [section boost/python/copy_const_reference.hpp]
2 [section Class `copy_const_reference`]
3 `copy_const_reference` is a model of [link concepts.resultconverter.resultconvertergenerator_concept ResultConverterGenerator] which can be used to wrap C++ functions returning a reference-to-const type such that the referenced value is copied into a new Python object.
5 namespace boost { namespace python
7 struct copy_const_reference
9 template <class T> struct apply;
14 [section Class `copy_const_reference` metafunctions]
15 ``template <class T> struct apply``
17 [[Requires][`T` is `U const&` for some `U`.]]
18 [[Returns][`typedef to_python_value<T> type;`]]
22 C++ module definition:
24 #include <boost/python/module.hpp>
25 #include <boost/python/class.hpp>
26 #include <boost/python/copy_const_reference.hpp>
27 #include <boost/python/return_value_policy.hpp>
33 Foo(int x) : { b.x = x; }
34 Bar const& get_bar() const { return b; }
40 using namespace boost::python;
41 BOOST_PYTHON_MODULE(my_module)
45 class_<Foo>("Foo", init<int>())
46 .def("get_bar", &Foo::get_bar
47 , return_value_policy<copy_const_reference>())
53 >>> from my_module import *
54 >>> f = Foo(3) # create a Foo object
55 >>> b = f.get_bar() # make a copy of the internal Bar object