boost/python/copy_const_reference.hpp

クラス

copy_const_reference クラステンプレート

struct copy_const_reference

copy_const_reference は、参照先の値を新しい Python オブジェクトにコピーする型への const 参照を返す C++ 関数をラップするのに使用する ResultConverterGenerator のモデルである。

copy_const_reference クラスの概要

namespace boost { namespace python
{
    struct copy_const_reference
    {
        template <class T> struct apply;
    };
}}

copy_const_reference クラスのメタ関数

template<class T>
struct apply
要件

ある U に対して TU const&

typedef to_python_value<T> type

C++ のモジュール定義
#include <boost/python/module.hpp>
#include <boost/python/class.hpp>
#include <boost/python/copy_const_reference.hpp>
#include <boost/python/return_value_policy.hpp>

// ラップするクラス群
struct Bar { int x; }

struct Foo {
   Foo(int x) : { b.x = x; }
   Bar const& get_bar() const { return b; }
 private:
   Bar b;
};

// ラッパコード
using namespace boost::python;
BOOST_PYTHON_MODULE(my_module)
{
    class_<Bar>("Bar");

     class_<Foo>("Foo", init<int>())
        .def("get_bar", &Foo::get_bar
            , return_value_policy<copy_const_reference>())
       ;
}
Python のコード
>>> from my_module import *
>>> f = Foo(3)         # Foo オブジェクトを作成
>>> b = f.get_bar()    # 内部的な Bar オブジェクトのコピーを作成