Re: boost.python iterators

Dirk Steenpass <[email protected]>
Newsgroups gmane.comp.lib.boost.langbinding,gmane.comp.python.c++
Message-ID <[email protected]>
On Tue, 16 Aug 2005 09:05:47 -0400, David Abrahams wrote:

> Dirk Steenpass <[email protected]> writes:
> 
>> Yep, that is neat and works. Though the default_call_policies cannot be
>> used as the wrapped iterator returns by reference.
> 
> That is not a foregone conclusion, but it may be true in your case.

I try to be more precise: the default_call_policies will work
for wrapped iterators that return non-pointer types, char const*, and
PyObject* by value. Or putting it differently, functions that return
references (aka automatically dereferenced pointer types) are not accepted
by default_call_policies.

Thus the following snippet is not supposed to compile
(and does not do so):

//-------------------------------------------
// default_call_policies
#include <boost/python.hpp>

class Coo
{
public:
    Coo(int x): c_(x) {}
    int const& get_c() const { return c_; }

private:
    int c_;
};

using namespace boost::python;
BOOST_PYTHON_MODULE(coo)
{
    class_<Coo>("Coo", init<int>())

        // Fails because of bad call policy.
        .def("get_c", &Coo::get_c,
             default_call_policies())
        ;
}

int main(void)
{ return 0; }
//-------------------------------------------

I'm rather tenacious on the topic because the behavior of another call
policy, namely return_internal_reference, keeps confusing me; most
likely because I miss some important point about call policies in general.

The following snippet illustrates that return_internal_reference can be
used for a simple user defined type but not for the fundamental int. I am
probably making a fool of myself, but I do not see why this is so.

I am using boost-1.33.0.

As usual, looking for enlightenment,

dirk

//------------------------------------------
// return_internal_reference
#include <boost/python.hpp>

struct C
{
    C(int x): c_(x) {}
    int c_;
};

class B
{
public:
    B(int x) : b_(x), c_(x) {}

    int const& get_b() const { return b_; }
    C   const& get_c() const { return c_; }

private:
    int b_;
    C c_;
};

using namespace boost::python;
BOOST_PYTHON_MODULE(b)
{
    class_<B>("B", init<int>())

        // Fundamental type, cannot be returned
        // by reference this way.
        .def("get_b", &B::get_b,
             return_internal_reference<>())

        // User defined type: compiles fine.
        .def("get_c", &B::get_c,
             return_internal_reference<>())
        ;
}

int main(void)
{ return 0; }






-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO
September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices
Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA
Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.