[C++-sig] boost python exposing const*
Holger Joukl
Holger.Joukl at LBBW.de
Wed Sep 9 10:04:30 CEST 2015
Hi,
> I have a c++ class T that I succesfully expose via:
>
> class_<T, T*> ...
>
> later on, I have
>
> struct S {
> const T* underlying;
> };
>
> class_<S,S*>("S").
> .def_readonly("underlying", &S::underlying)
> ;
>
> From python, when I call:
>
> s.underlying
>
> where s is a python instance of S, i get the following error:
>
> TypeError: No to_python (by-value) converter found for C++ type: T const*
>
> Do I need another converter for T const* in particular? or for both
> T* and T const*?
> Does the class_<T,T*> above only expose T itself, not pointers to it?
Using add_property() instead should be your friend, to be able to use a
call policy.
E.g.:
#include <boost/python.hpp>
namespace bp = boost::python;
// classes to expose
struct A
{
};
struct B
{
B() : underlying(NULL) { }
A const * underlying;
};
// expose to python
BOOST_PYTHON_MODULE(properties)
{
bp::class_<A>("A")
;
bp::class_<B>("B")
// Won't work: "No to_python (by-value) converter found...
//.def_readonly("underlying", &B::underlying)
.add_property("underlying", bp::make_getter(&B::underlying,
bp::return_internal_reference<>()))
;
}
See FAQ entry also:
http://www.boost.org/doc/libs/1_59_0/libs/python/doc/v2/faq.html#topythonconversionfailed
Holger
Landesbank Baden-Wuerttemberg
Anstalt des oeffentlichen Rechts
Hauptsitze: Stuttgart, Karlsruhe, Mannheim, Mainz
HRA 12704
Amtsgericht Stuttgart
More information about the Cplusplus-sig
mailing list