[C++-sig] Raw member and constructor functions
Studious Apprentice
apprentice_99 at hotmail.com
Mon Jun 6 10:32:40 CEST 2005
Thanks a lot for this fast answer ! This looks like the way to go. The
following code does compile but the result under python is unexpected :
#include <boost/python.hpp>
#include <boost/python/raw_function.hpp>
using namespace boost::python;
namespace {
class X {
public:
object memberraw(const tuple& args, const dict& kw)
{
return make_tuple(args, kw);
}
};
object memberrawfwd(tuple raw, dict kw)
{
tuple arg = extract<tuple&>(raw.slice(1, _))();
return extract<X&>(raw[0])().
memberraw(arg, kw);
}
};
BOOST_PYTHON_MODULE(PythonTest)
{
class_<X>("X")
.def("memberraw", raw_function(memberrawfwd))
;
}
However the following results when run with Python :
>>>a = X()
>>>a.memberraw()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: No registered converter was able to extract a C++ reference to
type c
lass boost::python::tuple from this Python object of type tuple
>>>
Any further help ? Thanks in advance,
Marc
-----Original Message-----
From: c++-sig-bounces at python.org [mailto:c++-sig-bounces at python.org] On
Behalf Of David Abrahams
Sent: lundi 6 juin 2005 04:31
To: c++-sig at python.org
Subject: Re: [C++-sig] Raw member and constructor functions
"Studious Apprentice" <apprentice_99 at hotmail.com> writes:
>Hello !
>
>I would like to use raw member functions and a raw constructor. However I
>can only manage to have raw functions to work.
>
>The following code works :
>
>#include <boost/python.hpp>
>#include <boost/python/raw_function.hpp>
>using namespace boost::python;
>
>tuple raw(tuple args, dict kw)
>{
> return make_tuple(args, kw);
>}
>
>BOOST_PYTHON_MODULE(PythonTest)
>{
> def("raw", raw_function(raw));
>}
>
>However this code does not compile with MSVC7.1 and results in "error
>C2064: term does not evaluate to a function taking 2 arguments" :
>
>#include <boost/python.hpp>
>#include <boost/python/raw_function.hpp>
>using namespace boost::python;
>
>class X {
>public:
> object memberraw(tuple args, dict kw)
> {
> return object();
> }
>};
>
>BOOST_PYTHON_MODULE(PythonTest)
>{
> class_<X>("X")
> .def("memberraw", raw_function(&X::memberraw))
> ;
>}
As http://www.boost.org/libs/python/doc/v2/raw_function.html says,
f(tuple(), dict())
must be well-formed where f is the argument to raw_function. That's
clearly not the case here. This might work:
tuple raw(tuple raw, dict kw)
{
extract<X&>(raw[0])().memberraw(raw.slice(1), kw);
}
BOOST_PYTHON_MODULE(PythonTest)
{
class_<X>("X")
.def("memberraw", raw_function(raw))
;
}
--
Dave Abrahams
Boost Consulting
www.boost-consulting.com
_______________________________________________
C++-sig mailing list
C++-sig at python.org
http://mail.python.org/mailman/listinfo/c++-sig
More information about the Cplusplus-sig
mailing list