Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to expose std::vector<int> as a Python list using SWIG?
    text
    copied!<p>I'm trying to expose this function to Python using SWIG:</p> <pre><code>std::vector&lt;int&gt; get_match_stats(); </code></pre> <p>And I want SWIG to generate wrapping code for Python so I can see it as a list of integers.</p> <p>Adding this to the .i file:</p> <pre> %include "typemaps.i" %include "std_vector.i" namespace std { %template(IntVector) vector&lt;int&gt;; } </pre> <p>I'm running <code>SWIG Version 1.3.36</code> and calling swig with <code>-Wall</code> and I get no warnings.</p> <p>I'm able to get access to a list but I get a bunch of warnings when compiling with <code>-Wall</code> (with <code>g++ (GCC) 4.2.4</code> ) the generated C++ code that say:</p> <pre> warning: dereferencing type-punned pointer will break strict-aliasing rules </pre> <p>Am I exposing the function correctly? If so, what does the warning mean?</p> <hr> <p>These are the lines before the offending line in the same function:</p> <pre> SWIGINTERN PyObject *_wrap_IntVector_erase__SWIG_0(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; std::vector *arg1 = (std::vector *) 0 ; std::vector::iterator arg2 ; std::vector::iterator result; void *argp1 = 0 ; int res1 = 0 ; swig::PySwigIterator *iter2 = 0 ; int res2 ; PyObject * obj0 = 0 ; PyObject * obj1 = 0 ; if (!PyArg_ParseTuple(args,(char *)"OO:IntVector_erase",&obj0,&obj1)) SWIG_fail; res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_std__vectorT_int_std__allocatorT_int_t_t, 0 | 0 ); if (!SWIG_IsOK(res1)) { SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "IntVector_erase" "', argument " "1"" of type '" "std::vector *""'"); } arg1 = reinterpret_cast * >(argp1); </pre> <p>And this is the offending line:</p> <pre> res2 = SWIG_ConvertPtr(obj1, SWIG_as_voidptrptr(&iter2), swig::PySwigIterator::descriptor(), 0); </pre> <p>More code follows that.</p> <p>The warning generated when compiling with g++ 4.2.4 is:</p> <pre> swig_iss_wrap.cxx: In function ‘PyObject* _wrap_IntVector_erase__SWIG_0(PyObject*, PyObject*)’: swig_iss_wrap.cxx:5885: warning: dereferencing type-punned pointer will break strict-aliasing rules </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