boostorg / boostorg/python

return_internal_reference not working for class with virtual function

Đang mở
#242 0 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
C++
Star
537
Fork
223
Merge trung bình
11 giờ 22 phút
Pull request đã merge (30 ngày)
2

Mô tả

I wrap the template classes A and B through boost python, and try to cast between them. I implement the toB function and wrap it with return_internal_reference<> to achieve this. However, it doesn't return the B object in python, but returns the "dead" A object, of which member function can't be used anymore.

#include
#include
using namespace boost::python;

struct Bar
{
virtual void hi() { std::cout << "Hi Bar." << std::endl; }
virtual ~Bar() {}
};

template
struct Foo : Bar
{
void set(T v) { val = v; }
T get() { return val; }
private:
T val;
};

using A = Foo;
using B = Foo;

B & toB(A & a) { return *reinterpret_cast(&a);}

BOOST_PYTHON_MODULE(example)
{
class_("A")
.def("set", &A::set)
.def("get", &A::get)
;
class_("B")
.def("set", &B::set)
.def("get", &B::get)
;
def("toB", &toB, return_internal_reference<>());
}
Python execution

>>> from example import *
>>> a = A()
>>> toB(a)

>>> toB(a).get()
Traceback (most recent call last):
File "", line 1 in
Boost.Python.ArgumentError: Python argument types in
A.get(A)
did not match C++ signature:
get(Foo {lvalue})

It works well if I remove the "virtual" keywords in Bar.

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.