如何在包装包含向量的模板类时让SWIG应用模板?

我试图使用SWIG来包装(在C#中)一些包含模板类的c ++代码,该模板类本身包含了一个std::vector 。 我已经在互联网上看到了关于如何为矢量类型声明模板的各种参考,但是无法使用额外的抽象层。 这就是我在interface.i文件中所做的事情,例如:

 // interface.i file %module myNamespaceWrapper %{ #include "myVector.h" %} %include "myVector.h" %include "std_string.i" %include "std_vector.i" namespace std { %template(vector_MyType) vector; %template(vector_Int) vector; } namespace myNamespace { %template(myVectorMyType) myVector; %template(myVectorInt) myVector; } 

虽然这似乎可以自己正确地创建相应的C#类型,但是我试图定义的std :: vector模板不会应用于其他在内部使用它们的类,通过头文件包含它们。 为了告诉你我的意思,有一个c ++类,如:

 // myVector.h namespace myNamespace { template class myVector { private : std::vector vect; public : myVector(){} virtual ~myVector(){} virtual const std::vector & getVect() { return vect; } virtual T& front() { return vect.front(); } } } 

即使我得到一个由SWIG创建的vector_MyType类,当它包装我的模板类时,它没有使用它,而是给出了很多这样的例子:

 public class myVectorMyType : global::System.IDisposable { public virtual SWIGTYPE_p_p_myNamespace__MyType front() { SWIGTYPE_p_p_myNamespace__MyType ret = new SWIGTYPE_p_p_myNamespace__MyType(myNamespaceWrapperPINVOKE.myVectorMyType_front__SWIG_0(swigCPtr), false); return ret; } public virtual SWIGTYPE_p_myNamespace__std__vectorT_myNamespace__MyType_p_t getVect() { SWIGTYPE_p_myNamespace__std__vectorT_myNamespace__MyType_p_t ret = new SWIGTYPE_p_myNamespace__std__vectorT_myNamespace__MyType_p_t(myNamespaceWrapperPINVOKE.myVectorMyType_getVect(swigCPtr), false); return ret; } } 

请有人可以告诉SWIG我缺少使用MyTypevector_MyType而不是SWIGTYPE_p_myNamespace__std__vectorT_myNamespace__MyType_p_tSWIGTYPE_p_p_myNamespace__MyType生成包装器?

这与模板getVect()函数返回T& ,即我需要对%template(myVectorInt) myVector;执行任何不同的操作%template(myVectorInt) myVector; case,哪里int不是指针?

我建议避免这个问题,只需为你需要的每个实例化创建包装器:

 class wrap_int : public temp  { }; 

最后我意识到我的错误,SWIG按预期生成了类。

 // interface.i file %module myNamespaceWrapper %{ #include "myVector.h" %} %include "myVector.h" %include "std_string.i" %include "std_vector.i" namespace std { %template(vector_MyType) vector; %template(vector_Int) vector; } namespace myNamespace { // using namespace std; // don't include this! %template(myVectorMyType) myVector; %template(myVectorInt) myVector; } 

这产生了如下类:

 public class myVectorMyType : global::System.IDisposable { private global::System.Runtime.InteropServices.HandleRef swigCPtr; protected bool swigCMemOwn; ... public virtual vector_MyType getVect() { vector_MyType ret = new vector_MyType(myNamespaceWrapperPINVOKE.myVectorMyType_getVect(swigCPtr), false); return ret; } } 

其中底层vector_MyType与std_vector.i模板一致:

 public class vector_MyType: global::System.IDisposable, global::System.Collections.IEnumerable , global::System.Collections.Generic.IList {...}