[C++-sig] Re: def_readonly and const array members

David Abrahams dave at boost-consulting.com
Fri Jul 25 17:38:12 CEST 2003


"Jeff Brewer" <jeff at brewer.com> writes:

> I am using Boost 1.30. Sorry I forgot to mention that.
>
> def_readonly seems to still in 1.30 and its implementation is:
> template <class D, class B>
> self& def_readonly(char const* name, D B::*pm_)
>     {
>         D T::*pm = pm_;
>         this->add_property(name, make_getter(pm));
>         return *this;
>     }
> Is it deprecated though?

Nope.  I don't have time to figure out why this isn't working for you
right now, but...

> However, I tried add_property with make_getter (which I usually use
> anyway) and I get the same error. Here is my new class_ code:
>
> class_<testStruct>("testStruct", init<>())
> 	.add_property("theArray",
> make_getter(&testStruct::theArray,
> return_value_policy<return_by_value>()));


Why not skip the to_python_converter, and instead build a "get"
function for a property, something like:

template <class D, unsigned N, class C, D (C::*pm)[N]>
get_array
{
     static tuple execute(C& c)
     {
          list l;
          for (unsigned i = 0; i < N; ++i)
              l.append((c.*pm)[N]);
          return tuple(l);
     }
};

class_<testStruct>("testStruct", init<>())
 	.add_property(
         "theArray"
       , &get_array<
             unsigned char
           , 16
           , testStruct
           , &testStruct::theArray
         >::execute
    );

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com





More information about the Cplusplus-sig mailing list