Swig 3.0.7,Python,std::vector<Class::Class>:包含 std_vector.i 在 std::vector 的分配器上添加额外的作用域

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

如何解决以下编译器错误:

鉴于:

%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"

%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

我在 QuadKey 命名空间中有一个名为 QuadKey 的 C++ class 在编译由 Swig 3.0.7 生成的 .cpp 文件时生成以下编译器错误(无论我是否使用模板,它都会执行此操作或不,仅包含 std_vector.i 将导致问题:

/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);
...

请注意,此模板在为 c# 或 java 生成代码时工作正常。它只打破 Python.

那是因为您在 namespace std 中定义了 %template。尝试

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

解决方案是将命名空间重命名为 QuadKeys。所以现在:

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

有效。