Note that there are some explanatory texts on larger screens.

plurals
  1. POauto_ptr with swig
    text
    copied!<p>I'm trying to wrap a C++ library which uses auto_ptr. I'm using swig and want to generate python bindings. I'v seen the section of the swig docu on how to use swig with smart pointers <a href="http://www.swig.org/Doc2.0/SWIGDocumentation.html#SWIGPlus_smart_pointers" rel="nofollow noreferrer">here</a>. But I can't get it to work. </p> <p>Swig generates code that wants to initialize the auto_ptr using a const reference, but auto_ptr defines the copy constructor with a non-const reference e.g. auto_ptr(auto_ptr &amp;). The generated code does not compile with "discards const qualifiers". When I manually delete the const qualifier the code compiles fine.</p> <p>I'v seen lots of mailing list entries but nothing helped. Can someone provide me with a working example. My non working sample is here: </p> <pre><code>%module auto_ptr_test %{ #include &lt;memory&gt; #include &lt;iostream&gt; using namespace std; %} namespace std { template &lt;class T&gt; class auto_ptr { auto_ptr(); auto_ptr(auto_ptr &amp;); T *operator-&gt;() const; }; } %inline %{ class Test { Test() { cout &lt;&lt; "Test()" &lt;&lt; endl; } public: static std::auto_ptr&lt;Test&gt; create() const { return auto_ptr&lt;Test&gt;(new Test()); } void greet() { cout &lt;&lt; "hello" &lt;&lt; endl; } }; %} %template () std::auto_ptr&lt;Test&gt;; </code></pre> <p>I compiled it using cmake with the following CMakeLists.txt:</p> <pre><code>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}) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload