2017-05-02 10 views
0

Boost.Python을 사용하여 개인 생성자로 C++ 클래스를 바인딩하려고합니다.Boost.Python을 사용하는 전용 생성자

class Test 
{ 
    public: 
     static Test& GetInstance() 
     { 
      static Test globalInstance; 
      return globalInstance; 
     } 

     int content(){return 0;} 

    private: 
     Test(){std::cout << "Object constructed" << std::endl;} 
}; 

이 클래스는 개인 생성자를 가지고 있으며, 클래스의 인스턴스를 얻기 위해, 우리는 GetInstance() 메서드를 호출해야합니다 :

하자 내가 다음과 같은 클래스가 있다고 가정합니다.

나는 다음과 같은 코드를 사용하여 바인딩을 시도 :

BOOST_PYTHON_MODULE(test) 
{ 
    class_<Test>("Test", no_init) 
     .def("create", &Test::GetInstance) 
     .def("content", &Test::content); 
} 

이 코드는 컴파일되지 않습니다 및이 오류가 나에게 제공합니다

  • /Sources/Boost/boost/python/detail/invoke.hpp:75:12: error: type 'const boost::python::detail::specify_a_return_value_policy_to_wrap_functions_returning<Test &>' does not provide a call operator
  • /Sources/Boost/boost/python/detail/caller.hpp:102:98: error: no member named 'get_pytype' in 'boost::python::detail::specify_a_return_value_policy_to_wrap_functions_returning<Test &>'

그러나호출하는 함수를 만드는 경우 다음과 같은210 :

Test create() 
{ 
    return Test::GetInstance(); 
} 

와 나는 내 바인딩에 .def("create", create)에 의해 .def("create", &Test::GetInstance)를 교체하는 것이, 모든 것이 잘 작동합니다.

공개 GetInstance() 메서드를 직접 사용할 수없는 이유는 무엇입니까?

답변

0

여기의 문제는 명시 적 반환 정책이없는 데서 실제로 발생합니다.

  • reference_existing_object
  • copy_non_const_reference
  • copy_const_reference
  • manage_new_object
  • return_by_value

: 함수/메소드는 값으로 반환하지 않을 경우, 우리는 중 하나에 그것의 반환 정책을 설정해야합니다 따라서 간단히 GetInstance() 메서드를 다음과 같이 바인딩합니다.

.def("create", &Test::GetInstance, return_value_policy<copy_non_const_reference>()) 

이 문제를 해결합니다.

누군가가 도움을 줄 수 있기를 바랍니다. 부스트의 오류 메시지가 여기 도움이되지 않습니다.