[C++-sig] Non-const reference arguments and from_python conversions

Bilokon, Paul paul.bilokon at lehman.com
Tue May 20 15:08:54 CEST 2008


P.S. I'm looking at

http://www.boost.org/doc/libs/1_35_0/libs/python/doc/v2/faq.html#questio
n2

And it says

Using custom rvalue converters. Boost.Python "rvalue converters" match
function signatures such as: 
void foo(std::vector<double> const& array); // pass by const-reference
void foo(std::vector<double> array); // pass by value

Does this mean the argument MUST be passed by const-reference or by
value (i.e. it can't be a non-const reference or a pointer)?

Many thanks,
Paul

> _____________________________________________ 
> From: 	Bilokon, Paul  
> Sent:	Tuesday, May 20, 2008 1:56 PM
> To:	Development of Python/C++ integration
> Subject:	Non-const reference arguments and from_python
> conversions
> 
> Hi all,
> 
> I have written a from_python_stream converter for Python streams (like
> sys.stdout).
> 
> The converter registers itself as follows:
> 
>         	boost::python::converter::registry::push_back(
>                 &convertible,
>                 &construct,
>                 boost::python::type_id<_Stream>());
> 
> So nothing unusual here. _Stream is the template argument.
> 
> Then I do the following:
> 
>     StreamConversions::from_python_stream<reference_wrapper<ostream>
> >();
> 
> Again, nothing unusual here.
> 
> I'm testing it on two functions:
> 
> void outTest(const ostream & out)
> {
> 	// cout << "Test-----outTest successful" << endl;
> }
> 
> void outTestRef(ostream & out)
> {
> 	cout << "Test-----outTest successful" << endl;
> }
> 
> Which are registered like this:
> 
>     def("outTest", outTest);
>     def("outTestRef", outTestRef);
> 
> Obviously I need outTestRef to work. Otherwise I can't use operator<<,
> so what's the point.
> 
> Unfortunately, outTest works, outTestRef does not work:
> 
> outTest(sys.stderr) # works
> outTestRef(sys.stderr) # fails
> 
> The error is:
> 
>   File "proto_test.py", line 23, in <module>
>     outTestRef(sys.stderr)
> Boost.Python.ArgumentError: Python argument types in
>     pyamp.proto.outTestRef(file)
> did not match C++ signature:
>     outTestRef(class std::basic_ostream<char,struct
> std::char_traits<char> > {lvalue})
> 
> Hence the question: is there absolutely no support for non-const
> reference arguments?
> 
> Many thanks for your help!
> 
> Regards,
> Paul
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

This message is intended only for the personal and confidential use of the designated recipient(s) named above.  If you are not the intended recipient of this message you are hereby notified that any review, dissemination, distribution or copying of this message is strictly prohibited.  This communication is for information purposes only and should not be regarded as an offer to sell or as a solicitation of an offer to buy any financial product, an official confirmation of any transaction, or as an official statement of Lehman Brothers.  Email transmission cannot be guaranteed to be secure or error-free.  Therefore, we do not represent that this information is complete or accurate and it should not be relied upon as such.  All information is subject to change without notice.






More information about the Cplusplus-sig mailing list