Matthew Hoggan
Matthew Hoggan

Reputation: 7594

Swig 3.0.7, Python, std::vector<Class::Class>: Inclusion of std_vector.i Adds Additional Scope on Allocator of std::vector

How do I resolve the compiler errors below:

Given:

%module SwigQuadKey

%{
#include "QuadKey/QuadKey.h"
%}

%include "operators.i"

%include <stdint.i>
%include <std_string.i>                           

%include "declspec.h"

%include "QuadKey/GeoCoordinate2d.h"              
%include "QuadKey/GeoCoordinateBoundingBox2d.h"   
%include "QuadKey/QuadKeyTypes.h"                 

%rename(GeoCoordinate2d) QuadKey::GeoCoordinate2d;

#if defined(SWIGPYTHON)                           
%include <std_vector.i>                           
%template(QuadKeyVector) std::vector<QuadKey::QuadKey>; 
#endif

%include "QuadKey/QuadKey.h"

Or

%module SwigQuadKey

%{
#include "QuadKey/QuadKey.h"
%}

%include "operators.i"

%include <stdint.i>
%include <std_string.i>                           

%include "declspec.h"

%include "QuadKey/GeoCoordinate2d.h"              
%include "QuadKey/GeoCoordinateBoundingBox2d.h"   
%include "QuadKey/QuadKeyTypes.h"                 

%rename(GeoCoordinate2d) QuadKey::GeoCoordinate2d;

%include "QuadKey/QuadKey.h"

#if defined(SWIGPYTHON)                           
%include <std_vector.i>                           
%template(QuadKeyVector) std::vector<QuadKey::QuadKey>; 
#endif

Where I have a C++ class named QuadKey inside a QuadKey namespace is generating the following compiler errors when compiling the .cpp file generated by Swig 3.0.7 (It does this regardless of if I use the template or not, solely including std_vector.i will cause the issue:

/home/mhoggan/Devel/QuadKeys/Swig/python/QuadKey_python.cpp: In 
function ‘PyObject* _wrap_QuadKey_getChildren(PyObject*, PyObject*)’:
/home/mhoggan/Devel/QuadKeys/Swig/python/QuadKey_python.cpp:10032:75: error: type/value mismatch at argument 1 in template parameter list for ‘template<class> class std::allocator’
   std::vector< QuadKey::QuadKey,std::allocator< QuadKey::QuadKey::QuadKey > > *arg2 = 0 ;
                                                                           ^
/home/mhoggan/Devel/QuadKeys/Swig/python/QuadKey_python.cpp:10032:75: error:   expected a type, got ‘QuadKey::QuadKey::QuadKey’
/home/mhoggan/Devel/QuadKeys/Swig/python/QuadKey_python.cpp:10032:77: error: template argument 2 is invalid
   std::vector< QuadKey::QuadKey,std::allocator< QuadKey::QuadKey::QuadKey > > *arg2 = 0 ;
                                                                             ^
/home/mhoggan/Devel/QuadKeys/Swig/python/QuadKey_python.cpp:10032:85: error: invalid type in declaration before ‘=’ token
   std::vector< QuadKey::QuadKey,std::allocator< QuadKey::QuadKey::QuadKey > > *arg2 = 0 ;
                                                                                     ^
/home/mhoggan/Devel/QuadKeys/Swig/python/QuadKey_python.cpp:10053:100: error: type/value mismatch at argument 1 in template parameter list for ‘template<class> class std::allocator’
   arg2 = reinterpret_cast< std::vector< QuadKey::QuadKey,std::allocator< QuadKey::QuadKey::QuadKey > > * >(argp2);
                                                                                                    ^
/home/mhoggan/Devel/QuadKeys/Swig/python/QuadKey_python.cpp:10053:100: error:   expected a type, got ‘QuadKey::QuadKey::QuadKey’
/home/mhoggan/Devel/QuadKeys/Swig/python/QuadKey_python.cpp:10053:102: error: template argument 2 is invalid
   arg2 = reinterpret_cast< std::vector< QuadKey::QuadKey,std::allocator< QuadKey::QuadKey::QuadKey > > * >(argp2);
                                                                                                      ^
/home/mhoggan/Devel/QuadKeys/Swig/python/QuadKey_python.cpp:10053:104: error: expected ‘>’ before ‘*’ token
   arg2 = reinterpret_cast< std::vector< QuadKey::QuadKey,std::allocator< QuadKey::QuadKey::QuadKey > > * >(argp2);
                                                                                                        ^
/home/mhoggan/Devel/QuadKeys/Swig/python/QuadKey_python.cpp:10053:104: error: expected ‘(’ before ‘*’ token
/home/mhoggan/Devel/QuadKeys/Swig/python/QuadKey_python.cpp:10053:106: error: expected primary-expression before ‘>’ token
   arg2 = reinterpret_cast< std::vector< QuadKey::QuadKey,std::allocator< QuadKey::QuadKey::QuadKey > > * >(argp2);
                                                                                                          ^
/home/mhoggan/Devel/QuadKeys/Swig/python/QuadKey_python.cpp:10053:114: error: expected ‘)’ before ‘;’ token
   arg2 = reinterpret_cast< std::vector< QuadKey::QuadKey,std::allocator< QuadKey::QuadKey::QuadKey > > * >(argp2);
...

Note that this template works fine when generating code for c# or for java. It only breaks with Python.

Upvotes: 0

Views: 175

Answers (2)

Matthew Hoggan
Matthew Hoggan

Reputation: 7594

The solution was to rename the namespace to QuadKeys. So now:

%template(QuadKeyVector) std::vector<QuadKeys::QuadKey>;

works.

Upvotes: 0

Alexander Solovets
Alexander Solovets

Reputation: 2507

That's because you define %template within namespace std. Try

%template(QuadKeyVector) vector<::QuadKey::QuadKey>;

Upvotes: 0

Related Questions