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

Langston, Matthew David langston at SLAC.Stanford.EDU
Mon Dec 9 01:55:34 CET 2002


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(); } };

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 > () );

Warmest regards, Matt

--
Matthew D. Langston
SLD, Stanford Linear Accelerator Center
langston at SLAC.Stanford.EDU





More information about the Cplusplus-sig mailing list