2017-11-29 20 views
1

Boost Python 1.63 (python 2.7.13)은 shared_ptr<T>과 잘 작동합니다. 나는 C++이를 작성하는 경우 : shared_ptr과 함께 Boost Python 사용 <const T>

shared_ptr<Foo> create_shared_ptr() { return shared_ptr{...}; } 
void accept_shared_ptr(const shared_ptr<Foo>& p) { } 

... 

class_<Foo, boost::noncopyable>("Foo", no_init); //Expose abstract class 

register_ptr_to_python< shared_ptr<Foo> >(); 
def("create_shared_ptr", create_shared_ptr); 
def("accept_shared_ptr", accept_shared_ptr); 

은 그 때 나는 파이썬이 쓸 수 및 모든 작동 : 나는 시도하고 shared_ptr<const Foo>을 포장 할 때

accept_shared_ptr(create_shared_ptr()) 

문제가 온다. (I이를 반환하는 라이브러리를 포장하고 있기 때문에 내가 할 필요가있다.) 다음과 같이 내가 ++ 기능을 C를 수정하는 경우 :

Boost.Python.ArgumentError: Python argument types in 
    mod_python.accept_shared_ptr(Foo) 
did not match C++ signature: 
    accept_shared_ptr(std::shared_ptr<Foo const>) 

그것은이 보인다

shared_ptr<const Foo> create_shared_ptr() { return shared_ptr{...}; } 
void accept_shared_ptr(const shared_ptr<const Foo>& p) { } 

는 다음 나는 오류 내부는 Python Foo에서 C++ shared_ptr<Foo>으로의 변환을 구현하고 있지만 C++ shared_ptr<const Foo>에는 변환을 구현하지 않습니다. 사용하기

register_ptr_to_python< shared_ptr<const Foo> >(); 

도움이되지 않습니다. 이 문제를 어떻게 해결할 수 있습니까?

+0

누가 투표를 종료 하든지 : 부스트 1.62에 기반한 아래의 대답으로 문제가 해결되지 않습니다. Boost 1.62는 9 월 1 일에 공개되었고, 1.62는 11 월 2 일에 릴리스되었습니다. (https://github.com/boostorg/python/commit/97e4b34a15978ca9d7c296da2de89b78bba4e0d5). 이 커밋이 무언가를 깨뜨린 것일 수 있습니다. – Mohan

답변

1

문제는 클래스/메소드의 정의에 있어야합니다. 이 코드는 (내가 부스트 1.62 및 파이썬 2.7.13이) 나를 위해 작동 :

class Foo { 
public: 
    virtual ~Foo() = default; 
    virtual std::string xxx() = 0; 
}; 

class Bar: public Foo { 
public: 
    ~Bar() override = default; 
    std::string xxx() override { return "xxx"; } 
}; 

std::shared_ptr<const Foo> create_shared_ptr() { 
    return std::shared_ptr<const Foo>(new Bar); 
} 

void accept_shared_ptr(const std::shared_ptr<const Foo>& p) 
{ 
    ; // do nothing 
} 

BOOST_PYTHON_MODULE(myLib) 
{ 
    using namespace boost::python; 
    class_<Foo, boost::noncopyable>("Foo", no_init); 
    register_ptr_to_python< std::shared_ptr<const Foo> >(); 
    def("create_shared_ptr", create_shared_ptr); 
    def("accept_shared_ptr", accept_shared_ptr); 
} 

다음, 파이썬에서 내가 할 수있는 :

$ python 
Python 2.7.13 (default, Jan 19 2017, 14:48:08) 
[GCC 6.3.0 20170118] on linux2 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import myLib 
>>> ptr = myLib.create_shared_ptr() 
>>> ptr 
<myLib.Foo object at 0x7f8b5fde0aa0> 
>>> myLib.accept_shared_ptr(ptr) 

대부분의 아마 당신의 기능 create_shared_ptr 어떻게 든 잘못된 값을 반환 파이썬에 의해 오해됩니다.

implicitly_convertible<std::shared_ptr<Foo>,std::shared_ptr<const Foo>>(); 

추가

+0

흥미 롭습니다. 귀하의 C++ 코드는 나를 위해 같은 오류가 발생 는 '' myLib.accept_shared_ptr (푸)에서'파이썬 인수 형식 가 C 일치하지 않습니다 ++ 서명 : accept_shared_ptr (표준 : : shared_ptr의 은)''' – Mohan

+0

내가 무엇을 물어볼 수 컴파일러를 사용하고 있습니까? 나는 g ++ 6.4를 사용하고'-std = C++ 11'로 컴파일하고있다. 또한 더 많은 전환을 허용 할 수있는 헤더가 포함되어 있습니까? – Mohan

+1

'g ++ (Debian 6.3.0-18) 6.3.0 20170516','-std = C++ 1y' (저는 C++ 14이어야합니다),'cmake version 3.7.2'. 부스트에는'#include '가 포함되어 있었지만 실제로 OpenCV 의존성을 포함하는 다른 프로젝트에 샘플 코드를 임베드했습니다. 나는 그 문제가 해결 된 것을 본다 - 좋은 (어쩌면 조금 해킹 되더라도). – Ptaq666

0

문제를 해결합니다.