[C++-sig] Re: operator[]

chuzo okuda okuda1 at llnl.gov
Wed May 22 19:01:23 CEST 2002


Sorry, I meant:

If I replace the class definition of operator[] in
double& operator[](const int i) {return i==0 ? x : y;}
by
double operator[](const int i) {return i==0 ? x : y;}

and the line
.def("__getitem__", (double& (Vector2d::*)(const int))
&Vector2d::operator[])
by either
.def("__getitem__", &Vector2d::operator[])
or
.def("__getitem__", (double (Vector2d::*)(const int) const)
&Vector2d::operator[])

Somehow I missed copying and pasting lines...
Chuzo




chuzo okuda wrote:
> 
> I am not sure how to fix the following compiler problem...
> 
> ------------------------------------------------
> #include <boost/python/module.hpp>
> #include <boost/python/class.hpp>
> using namespace boost::python;
> 
> class Vector2d {
> public:
>    Vector2d():x(0.0),y(0.0) {}
>    Vector2d(const double xval, const double yval): x(xval),y(yval) {}
>    ~Vector2d() {}
>    double& operator[](const int i) {return i==0 ? x : y;}
> 
> protected:
>    double x, y;
> };
> 
> BOOST_PYTHON_MODULE_INIT(simpleVector2)
> {
>    module Vect2dMod("simpleVector2");
>    Vect2dMod
>       .add(
>            class_<Vector2d>("Vector2d")
>            .def_init()
>            .def_init(args<const double, const double>())
>            //.def("__getitem__", &Vector2d::operator[])
>            .def("__getitem__", (double& (Vector2d::*)(const int))
> &Vector2d::operator[])
>            )
>       ;
> }
> -------------------------------------------------
> 
> Problem:
> If I replace the class definition of operator[] in
> double& operator[](int i) const {return i==0 ? x : y;}
> by either
> .def("__getitem__", &Vector2d::operator[])
> or
> .def("__getitem__", (double (Vector2d::*)(const int) const)
> &Vector2d::operator[])
> it compiled, but the above code issued error message:
> 
> "/usr/dnta/kull/developers/thirdPartySoftware/boost-chuzo/boost/boost/python/preprocessed/returning_non_void.hpp",
> line 35: error:
>           class
> 
> "boost::python::detail::specify_a_result_policy_to_wrap_functions_ret
>           urning<double &>" has no member "convertible"
>       if(!cr.convertible())return 0;
> 
> AND
> 
> "/usr/dnta/kull/developers/thirdPartySoftware/boost-chuzo/boost/boost/python/preprocessed/returning_non_void.hpp",
> line 37: error:
>           expression must have (pointer-to-) function type
>       PyObject*result=cr(((
> 
> ???
> Does anyone have any idea how to fix this?
> Thank you
> Chuzo
> 
> David Abrahams wrote:
> >
> > __getitem__ can be used directly, but the signature for __setitem__ is:
> >
> >     __setitem__(self, key, value):
> >
> > so you need a wrapper function:
> >
> >     template <class Self, class Key, class Value>
> >     setitem(Self& self, Key const& key, Value const& value)
> >     {
> >         self[key] = value;
> >     }
> >
> >     ...
> >
> >     .def("__setitem__", &setitem<Vector2d,int,double>)
> >
> > ----- Original Message -----
> > From: "chuzo okuda" <okuda1 at llnl.gov>
> > To: "David Abrahams" <david.abrahams at rcn.com>
> > Cc: "Martin Casado" <casado2 at llnl.gov>; "Susan Hazlett"
> > <shazlett at llnl.gov>; "Kathleen McCandless" <mccandless2 at llnl.gov>
> > Sent: Wednesday, April 24, 2002 7:38 PM
> > Subject: operator[]
> >
> > > Dave,
> > > Now, I have problem with operator[].
> > >
> > > class Vector2d {
> > > public:
> > >    Vector2d():x(0.0),y(0.0) {}
> > >    Vector2d(const double xval, const double yval): x(xval),y(yval) {}
> > >    ~Vector2d() {}
> > >    double getX() {return x;}
> > >    double getY() {return y;}
> > >    double& operator[](int i) {return i==0 ? x : y;}
> > >
> > > protected:
> > >    double x, y;
> > > };
> > >
> > > int main(int argc, char** argv) {
> > >    Vector2d v = Vector2d(1.0,2.0);
> > >    cout << v[0] << "; " << v[1] << endl;
> > >    v[0] = 5.0; v[1] = 9.0;
> > >    cout << v[0] << "; " << v[1] << endl;
> > > }
> > >
> > > And operator[] in C++ support both read and write operation.
> > > It is not clear how to write "__setitem__" and "__getitem__" like:
> > >
> > > .def("__setitem__", &Vector2d::operator[])
> > > .def("__getitem__", &Vector2d::operator[])
> >
> > _______________________________________________
> > C++-sig mailing list
> > C++-sig at python.org
> > http://mail.python.org/mailman/listinfo/c++-sig
> 
> _______________________________________________
> 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