boostorg / boostorg/python

return_internal_reference not working for class with virtual function

未關閉
#242 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視
主要語言
C++
星號
537
分支
223
平均合併
11 小時 22 分鐘
30 天內合併 PR
2

描述

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.

貢獻指南

這個儲存庫沒有索引到貢獻指南

研究方向

The payload names no repository files or tests. Start by compiling the minimal Boost.Python reproducer and inspect the toB binding, return_internal_reference<> behavior, and the A/B class wrappers; compare runs with and without Bar's virtual methods. Done means the conversion behavior is explained and an appropriate fix or supported-usage conclusion is established.

由索引模型根據 Issue 內容生成。

評估

技術堆疊
cpp, python
領域
api
Issue 類型
缺陷
難度
5/5
預估耗時
一週以上
活躍度
停滯
描述清晰度
基本清楚
新手友好度
25/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。