间歇性的错误返回与Boost.Python的内部参考 [英] intermittent error returning an internal reference with boost.python

查看:203
本文介绍了间歇性的错误返回与Boost.Python的内部参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类:

#include <array>

template<unsigned short D>
class Point {
private:
    std::array<float, D> coordinates;
public:
    Point() { for(int i=D-1; i>=0; --i) coordinates[i] = 0.0; }
    Point(const Point& rhs) = default;
    Point& operator=(const Point& rhs) = default;
    ~Point() = default;

    float& get_ref(const unsigned short dimension)
        { return coordinates[dimension-1]; }
};

我想用把它包起来:

I'm trying to wrap it with:

#include <boost/python.hpp>

BOOST_PYTHON_MODULE(fernpy) {
    using namespace boost::python;

    class_< Point<2> >("point")
        .def("__call__", &Point<2>::get_ref, return_internal_reference<>());
}

我用gcc-4.7编译为提升1.48,蟒蛇 - 2.7在Fedora 17的所有code是一个名为testpy.cpp文件。我使用这些命令编译:

I'm using gcc-4.7 to compile for boost 1.48, python-2.7 on Fedora 17. All the code is a file called testpy.cpp. I'm using these commands to compile:

g++ -std=c++11 -g -fPIC -I/usr/include/python2.7 -c testpy.cpp
g++ -shared -g -lpython2.7 -lboost_python -o libfern.so testpy.o

编译器返回一串提升内部错误,太多的张贴在这里。本文节选似乎是它的核心。还有之前的从要求一帮和注意S之后。

The compiler returns a bunch of boost internal errors, too many to post here. This excerpt seems to be the core of it. There are a bunch of "required from"s before it and "note"s after.

/usr/include/boost/python/object/make_instance.hpp:27:9: error: no matching function for call to ‘assertion_failed(mpl_::failed************ boost::mpl::or_<boost::is_class<float>, boost::is_union<float>, mpl_::bool_<false>, mpl_::bool_<false>, mpl_::bool_<false> >::************)’

>从包装的.DEF线()参数;

如果我返回从get_ref普通浮法并删除return_internal_reference&LT它工作得很好。这是奇怪,因为我在做另一个,更复杂的类模板同样的事情,它工作得很好那里。我一直在谷歌上搜索,现在敲我的头这几乎一整天。任何人有任何想法究竟发生了什么事?

It works just fine if I return a plain float from get_ref and remove the return_internal_reference<>() argument from the .def line of the wrapper. It's weird because I'm doing the same thing with another, more complicated class template and it works just fine there too. I've been Googling and banging my head against this for almost a full day now. Anybody have any idea what the heck is going on?

更新:

我结束了使用的GetItem,并从蟒蛇setitem特殊方法,一拉的这个链接。链接显示了如何定义与功能的访问静态包装一个漂亮的模板结构,所以你不必惹接口到原来的C ++类。

I ended up using the "getitem" and "setitem" special methods from python, a la this link. The link shows how to define a nifty struct template with static wrappers for access functions, so you don't have to mess with the interface to your original C++ class.

推荐答案

在Python点的视图,浮动是一个不可变型。因此,蟒蛇不允许更改值。

From a python point-of-view, floats are an immutable-type. As such, python does not allow changing the value.

例如,下面在python发生:

For example, the following occurs in python:

coordinates = [ 5, 10, 15 ]
x = cooardinates[ 2 ] # Bind x to refer to the int(15) object.
x = 5                 # Rebind x to refer to the int(5) object. 
                      # Does not modify coordinates.

现在,考虑以下几点:

from fernpy import point
p = point()
x = p(2) # Bind x to refer to the float(p(2)) object.
x = 5    # Rebind x to refer to the int(5) object.
         # Does not set p.coordinates[2] to 5.

因此​​,的boost ::蟒蛇 prevents到类型,将在蟒蛇一成不变因为Python不支持它返回引用。 X 不存储值 5 ;相反,它包含对 5 对象的引用。如果分配给 X 并没有重新绑定 X ,然后无厘头语句,如 6 = 5 将成为可能。

Thus, boost::python prevents returning reference to types that will be immutable in python because Python does not support it. x does not store the value 5; instead, it contains a reference to the 5 object. If assigning to x did not rebind x, then nonsensical statements, such as 6 = 5 would be possible.

编译错误是一个静态的检查,限制 return_internal_reference 只与类或工会工作,因为这些会在Python的可变类型。我想,这是工作返回到用户类型的引用了更复杂的类模板。

The compile error is a static check that limits return_internal_reference to only work with classes or unions, as those will be mutable types within Python. I imagine that the 'more complicated class template' that works is returning a reference to a user-type.

这篇关于间歇性的错误返回与Boost.Python的内部参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆