[C++-sig] contained structure access
Graeme Lufkin
gwl at u.washington.edu
Thu Nov 7 21:07:43 CET 2002
>> I tried
>> .def("__getitem__", &MyClass::operator[])
>> but it complained about sizeof() being applied to an unspecified
>> type.
>
>Can you show the complete error message?
Aha, I wasn't being precise enough. My operator[] returns a reference,
so that I can do things like
m = MyClass()
m[0] = 3.14
m[2] = 2.71828
If I change my operator[] to return a value, the .def() works, and I can
access the values. If I leave it as returning a reference, and use
return_internal_reference<>, it doesn't compile. Here's the code:
struct Atom {
double x, y, z;
double& operator[](const int i) {
switch(i) {
case 0: return x;
case 1: return y;
case 2: return z;
}
return x;
}
};
class_<Atom>("Atom")
.def_readwrite("x", &Atom::x)
.def_readwrite("y", &Atom::y)
.def_readwrite("z", &Atom::z)
.def("__getitem__", &Atom::operator[], return_internal_reference<>());
Given this, I get a long error chain with tons of templated goodness,
the last few 'instantied from's of which follow:
/net/mondo-1/nbody/gwl/projects/boost_1_29_0/boost/python/class.hpp:325: instantiated from `boost::python::class_<T, X1, X2, X3>::def_impl (const char *, Fn, const Keywords &, const Policies &, const char *, ...) [with Fn = double &(Atom::*) (int), Policies = boost::python::return_internal_reference<1, boost::python::default_call_policies>, Keywords = boost::python::detail::keywords<0>, T = Atom, X1 = boost::python::detail::not_specified, X2 = boost::python::detail::not_specified, X3 = boost::python::detail::not_specified]'
/net/mondo-1/nbody/gwl/projects/boost_1_29_0/boost/python/class.hpp:369: instantiated from `boost::python::class_<T, X1, X2, X3>::dispatch_def (const void *, const char *, Fn, const A1 &) [with Fn = double &(Atom::*) (int), A1 = boost::python::return_internal_reference<1, boost::python::default_call_policies>, T = Atom, X1 = boost::python::detail::not_specified, X2 = boost::python::detail::not_specified, X3 = boost::python::detail::not_specified]'
/net/mondo-1/nbody/gwl/projects/boost_1_29_0/boost/python/class.hpp:227: instantiated from `boost::python::class_<T, X1, X2, X3>::def (const char *, Arg1T, const Arg2T &) [with Arg1T = double &(Atom::*) (int), Arg2T = boost::python::return_internal_reference<1, boost::python::default_call_policies>, T = Atom, X1 = boost::python::detail::not_specified, X2 = boost::python::detail::not_specified, X3 = boost::python::detail::not_specified]'
TestModule.cpp:26: instantiated from here
/net/mondo-1/nbody/gwl/projects/boost_1_29_0/boost/python/object/make_instance.hpp:22: `sizeof' applied
to incomplete type `boost::STATIC_ASSERTION_FAILURE<false>'
g++ -c -Wall -ftemplate-depth-100 -DBOOST_PYTHON_DYNAMIC_LIB -g
-O0 -fno-inline -fPIC -I"../../../libs/python/TestModule" -isystem
"/usr/include/python2.2" -isystem
"/net/mondo-1/nbody/gwl/projects/boost_1_29_0" -o
"../../../libs/python/TestModule/bin/TestModule.so/gcc/debug/runtime-link-dynamic/shared-linkable-true/TestModule.o" "TestModule.cpp"
Hey, you asked for it! Seriously, if you want the whole thing, I'll
email it to you directly.
The other question: I'm still not sure I understand the business with
properties and return_internal_reference<>. I don't get why the extra
level of member-ness (i.e. x.y.z) reveals the copy business, which
doesn't appear one level lower (i.e. x.y). But I can make it work, so
maybe I'll just forget it.
Regarding the *= float(), I'm using python 2.2, and will live with
doing 'v = v * 2' until I can upgrade.
Also, I commend you (Dave) on your ridiculously fast response time.
You're making my life easier.
--
- Graeme Lufkin
gwl at u.washington.edu
"The experiment was inconclusive, so we had to use statistics."
More information about the Cplusplus-sig
mailing list