[C++-sig] shouldn't class_::add_property accept a return_value_policy?

David Abrahams dave at boost-consulting.com
Mon Dec 9 03:19:38 CET 2002


"Langston, Matthew David" <langston at SLAC.Stanford.EDU> writes:

> I have exposed a small C++ class library to Python using
> Boost.Python. I exposed all of the getters and setters as python
> properties (using class_::add_property), but since all of the getters
> returned const references I had to make a derived class for every
> original class:
>
> class Base { public: const std::string& get_Name(); };
>
> // Can't do this: // // class_< Base > ( "Base" ).add_property(
> "Name", &Base::get_Name ); // // The previous line produced the
> following error message // (using vc7 on win xp): // //
> ..\returning.hpp(175): error C2079: 'cr' uses undefined struct //
> 'boost::python::detail::specify_a_result_policy_to_wrap_functions_returning<T>'
> // with // [ // T=const std::basic_string<char,std::char_traits<char>,
> // std::allocator<char>> & // ]
>
> Instead I had to do the following for every class in the library:
>
> class Derived { public: std::string get_Name() {return Base::
> get_Name(); } };

There is an easier way:

   http://mail.python.org/pipermail/c++-sig/2002-December/002814.html

> Shouldn't class_::add_property be overloaded to take a
> return_value_policy so that I could do this:
>
> class_< Base > ( "Base" ) .add_property( "Name", &Base::get_Name,
> return_value_policy < copy_const_reference > () );

No, it would need two, to deal with getters and setters. The interface
gets too complicated that way. The granular approach outlined in the
link above is better, IMO.

-- 
                       David Abrahams
   dave at boost-consulting.com * http://www.boost-consulting.com
Boost support, enhancements, training, and commercial distribution





More information about the Cplusplus-sig mailing list