Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

Boost.Python - <boost/python/return_by_value.hpp>

Boost.Python

Header

Contents

Classes
Class return_by_value
Class return_by_value synopsis
Class return_by_value metafunctions
Example

Classes

Class return_by_value

return_by_value is a model of ResultConverterGenerator which can be used to wrap C++ functions returning any reference or value type such that the return value is copied into a new Python object.

Class return_by_value synopsis

namespace boost { namespace python
{
    struct return_by_value
    {
        template  struct apply;
    };
}}

Class return_by_value metafunctions

template  struct apply
Returns: typedef to_python_value type;

Example

C++ Module Definition

#include 
#include 
#include 
#include 

// classes to wrap
struct Bar { };

Bar global_bar;

// functions to wrap:
Bar b1();
Bar& b2();
Bar const& b3();

// Wrapper code
using namespace boost::python;
template 
void def_void_function(char const* name, R (*f)())
{
   def(name, f, return_value_policy());
}

BOOST_PYTHON_MODULE(my_module)
{
    class_("Bar");
    def_void_function("b1", b1);
    def_void_function("b2", b2);
    def_void_function("b3", b3);
}

Python Code

>>> from my_module import *
>>> b = b1() # each of these calls
>>> b = b2() # creates a brand
>>> b = b3() # new Bar object

Revised 13 November, 2002

© Copyright Dave Abrahams 2002.