[C++-sig] How To Determine Return Policy???
chuzo okuda
okuda1 at llnl.gov
Fri May 31 21:33:17 CEST 2002
I would like to know how to set correct return policy? It took me quite
a while to narrow down to where the problem was. So, my question is:
How do I determine which return policy should I be using???
In the example of test/test_pointer_adoption.cpp, it uses
return_internal_reference as in:
.def("get_inner", &A::get_inner, return_internal_reference<>())
and
struct A {
<snip>
inner& get_inner() {return x;}
inner x;
};
Initially I used return_internal_reference because of the above example
and if you replace inner with string in struct A, it made sense to me to
use return_internal_reference...
Here are codes that must use a specific return policy with my
comments...
------------ source code ------------------
#include <boost/python/module.hpp>
#include <boost/python/class.hpp>
#include <boost/python/return_internal_reference.hpp>
#include <boost/python/copy_non_const_reference.hpp>
#include <boost/python/return_value_policy.hpp>
class Tag {
public:
Tag():mName("") {}
Tag(const std::string& name):mName(name) {}
~Tag() {}
const std::string& name() const {return mName;}
private:
std::string mName;
};
BOOST_PYTHON_MODULE_INIT(Test2)
{
using namespace boost::python;
module TagMod("Test2");
TagMod
.add(
class_<Tag> ("Tag")
.def_init()
.def_init(args<const std::string&>())
// this line causes compiling errors
//.def("show", &Tag::name)
// return_internal_reference compiles, t=Tag("hello") creates
// instances, but t.show() cause TypeError.
//.def("show", &Tag::name, return_internal_reference<>())
// this one works alright...
.def("show", &Tag::name,
return_value_policy<copy_non_const_reference>())
)
;
}
More information about the Cplusplus-sig
mailing list