[C++-sig] lvalue converter - pass by reference (non-const)

Moe Goldberg goldberg at coredp.com
Wed May 14 22:55:25 CEST 2008


Hi
I was trying to pass an object by reference from python to C++ so that I 
could modify the object in C++. I am using the custom_string as an 
example for reference, and I am unable to get it to work.

Instead of showing you my case, which is complicated, I will demonstrate 
with the custom_string example.

If I change the definition of the size function from:
std::size_t size(custom_string const& s) { return s.value().size(); }
to
std::size_t size(custom_string& s) { return s.value().size(); }
(i.e. remove the const from s),
and then try to run it as follows:

 >>> import custom_string
 >>> mystr="123"
 >>> custom_string.size(mystr)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
Boost.Python.ArgumentError: Python argument types in
    custom_string.size(str)
did not match C++ signature:
    size(sandbox::(anonymous namespace)::custom_string {lvalue})

If both to_python and lvalue converters are registered, then why can't I 
do this? Or more importantly, how can I get this to work?

Thanks!
-MG



More information about the Cplusplus-sig mailing list