我想包裝一個使用auto_ptr的C++庫。我正在使用swig 並希望生成python綁定。我看過關於如何在智能指針here上使用swig的swig文件部分。但我無法讓它工作。帶swig的auto_ptr
痛飲生成希望使用一個const 參考初始化auto_ptr的代碼,但auto_ptr的限定具有非const 參考拷貝構造例如auto_ptr(auto_ptr &)。生成的 代碼不會使用「丟棄const限定符」進行編譯。當我手動 刪除const限定符代碼編譯罰款。
我看過很多郵件列表條目,但沒有任何幫助。有人 可以給我一個工作的例子。我不工作的樣品是在這裏:
%module auto_ptr_test
%{
#include <memory>
#include <iostream>
using namespace std;
%}
namespace std {
template <class T>
class auto_ptr {
auto_ptr();
auto_ptr(auto_ptr &);
T *operator->() const;
};
}
%inline %{
class Test {
Test() {
cout << "Test()" << endl;
}
public:
static std::auto_ptr<Test> create() const {
return auto_ptr<Test>(new Test());
}
void greet() {
cout << "hello" << endl;
}
};
%}
%template() std::auto_ptr<Test>;
我用用下面的CMakeLists.txt cmake的編譯它:
cmake_minimum_required(VERSION 2.8)
find_package(SWIG REQUIRED)
include(${SWIG_USE_FILE})
FIND_PACKAGE(PythonLibs)
INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_PATH})
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
SET(CMAKE_SWIG_FLAGS "")
SET_SOURCE_FILES_PROPERTIES(auto_ptr_test.i PROPERTIES CPLUSPLUS ON)
SWIG_ADD_MODULE(auto_ptr_test python auto_ptr_test.i)
SWIG_LINK_LIBRARIES(auto_ptr_test ${PYTHON_LIBRARIES})