[C++-sig] non const std::string* as argument

Roman Yakovenko roman.yakovenko at gmail.com
Thu Aug 21 11:38:13 CEST 2008


On Thu, Aug 21, 2008 at 12:30 PM, Max Ivanov <ivanov.maxim at gmail.com> wrote:
> Hi all!
> How to export to python class members like "bool
> SerializeToString(string* output) const"? In python strings are
> immutables, so there is no way to export this method directly. So I
> tried to write a wrapper:
>
> std::string&    wrap_SerializeToString(const gp::Message& msg) {
>        std::string tmp;
>        msg.SerializeToString(&tmp);
>        return tmp;
> };

Just remove reference from return type and you should be fine.

> and then  how I use it.

Like any other class method.

> Message class is base class for Link which I
> try to export to python, but Link never overrides SerializeToString.

What?

> bp::class_<gp::Message, boost::noncopyable>("Message", bp::no_init)
>            .def("SerializeToString", wrap_SerializeToString,
>                            bp::return_value_policy<bp::copy_const_reference())
>
> gcc errors are following:
> bindings.cpp: In function 'std::string& wrap_SerializeToString(const
> google::protobuf::Message&)':
> bindings.cpp:21: warning: reference to local variable 'tmp' returned
> bindings.cpp: In function 'void init_module__spidermsg()':
> bindings.cpp:46: error: a call to a constructor cannot appear in a
> constant-expression
> bindings.cpp:46: error: template argument 1 is invalid

Take a look on Py++ "Function Transformation" feature:
http://language-binding.net/pyplusplus/documentation/functions/transformation/transformation.html

It deals with such "wrappers".

-- 
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/



More information about the Cplusplus-sig mailing list