return_internal_reference not working for class with virtual function
- Linguagem predominante
- C++
- Estrelas
- 537
- Forks
- 223
- Merge médio
- 11h 22min
- PRs com merge (30d)
- 2
Descrição
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.
Guia de contribuição
Nenhum guia de contribuição indexado para este repositório
Direção de pesquisa
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.
Escrita pelo modelo de indexação a partir do texto da issue.
Avaliação
- Stack de tecnologia
- cpp, python
- Domínio
- api
- Tipo de issue
- Bug
- Dificuldade
- 5/5
- Tempo estimado
- Mais de uma semana
- Status de atividade
- Estagnada
- Clareza
- Razoavelmente clara
- Facilidade para iniciantes
- 25/100