[C++-sig] class_::add_property flexiablity

Jeff Holle jeff.holle at verizon.net
Tue Dec 3 05:50:54 CET 2002


c++-sig-request at python.org wrote:

>Send C++-sig mailing list submissions to
>	c++-sig at python.org
>
>To subscribe or unsubscribe via the World Wide Web, visit
>	http://mail.python.org/mailman/listinfo/c++-sig
>or, via email, send a message with subject or body 'help' to
>	c++-sig-request at python.org
>
>You can reach the person managing the list at
>	c++-sig-admin at python.org
>
>When replying, please edit your Subject line so it is more specific
>than "Re: Contents of C++-sig digest..."
>
>
>Today's Topics:
>
>   1. class_::add_property flexiablity (Jeff Holle)
>   2. Re: class_::add_property flexiablity (David Abrahams)
>   3. Re: call_method (David Abrahams)
>   4. Can't convert from class_ to scope in latest CVS! (Kerim Borchaev)
>   5. Re: Can't convert from class_ to scope in latest CVS! (David Abrahams)
>   6. Re: Can't convert from class_ to scope in latest CVS! (David Abrahams)
>
>--__--__--
>
>Message: 1
>Date: Sun, 01 Dec 2002 16:01:31 -0800
>From: Jeff Holle <jeff.holle at verizon.net>
>To:  c++-sig at python.org
>Subject: [C++-sig] class_::add_property flexiablity
>Reply-To: c++-sig at python.org
>
>I'm using gcc 3.2 and boost 1.29.0 on Mandrake Linux 9.0.
>
>I'm wondering if its possible to use add_property with an accessor that 
>returns a reference.
>
>The following code compiles, but if I change the Widget::getName method 
>to return a "const std::string&" it produces a lot of compiler errors....
>
>    #include <boost/python/module.hpp>
>    #include <boost/python/def.hpp>
>    #include <boost/python/class.hpp>
>    #include <string>
>
>    using namespace boost::python;
>
>    class Widget {
>    public:
>        Widget() {;}
>        const std::string getName(void) const { return m_Name;}
>    private:
>        std::string m_Name;
>    };
>
>
>    BOOST_PYTHON_MODULE(TestAddProperty)
>    {
>         class_<Widget>("Widget")
>            .add_property("Name",&Widget::getName);
>
>    }
>
>
>In reading your referenced html doc pages, I "fell into" this attempt:
>
>--__--__--
>
>Message: 2
>To: c++-sig at python.org
>Subject: Re: [C++-sig] class_::add_property flexiablity
>From: David Abrahams <dave at boost-consulting.com>
>Date: Sun, 01 Dec 2002 19:27:54 -0500
>Reply-To: c++-sig at python.org
>
>Jeff Holle <jeff.holle at verizon.net> writes:
>In reading your referenced html pages, I "fell into" this:
>
    .add_property("Name",make_function(&Widget::getName,
    return_value_policy<copy_const_reference>()));

It compiles without errors, but produces unacceptable runtime behaviours.
Amongest them is that this doesn't produce an error:

import TestAddProperty
widget = TestAddProperty.Widget
widget.Name = "this is a test"
print widget.Name
  

Note that I haven't provided a mutator.

I've subsequently investigated enough to know that my C++ method is not 
being called.

What is the correct thing to do?

The other question is why did this obviously incorrect thing compile?
Given this behaviour, the documentation needs to be much better...

  



>>I'm using gcc 3.2 and boost 1.29.0 on Mandrake Linux 9.0.
>>
>>I'm wondering if its possible to use add_property with an accessor that returns a reference.
>>
>>The following code compiles, but if I change the Widget::getName method to return a "const
>>std::string&" it produces a lot of compiler errors....
>>
>>    #include <boost/python/module.hpp>
>>    #include <boost/python/def.hpp>
>>    #include <boost/python/class.hpp>
>>    #include <string>
>>
>>    using namespace boost::python;
>>
>>    class Widget {
>>    public:
>>        Widget() {;}
>>        const std::string getName(void) const { return m_Name;}
>>    private:
>>        std::string m_Name;
>>    };
>>
>>
>>    BOOST_PYTHON_MODULE(TestAddProperty)
>>    {
>>         class_<Widget>("Widget")
>>            .add_property("Name",&Widget::getName);
>>
>>    }
>>    
>>
>
>Sure. The add_property docs at
>http://www.boost.org/libs/python/doc/v2/class.html#class_-spec-modifiers
>say:
>
>    Creates a new Python property class instance, passing object(fget)
>    (and object(fset) in the second form) to its constructor, then
>    adds that property to the Python class object under construction
>    with the given attribute name.
>
>So fget can be an instance of object already. How do you make a
>callable Python object which wraps a C++ function returning by
>reference? See
>http://www.boost.org/libs/python/doc/v2/make_function.html#make_function-spec.
>
>  
>







More information about the Cplusplus-sig mailing list