我有一个模板化的 Vector(如数学向量,而不是 std::vector)类,它可以用 2 到 4 之间的大小实例化。定义类似于:
template <uint32_t Size, typename StorageType>
class Vector
{
public:
Vector(StorageType x, StorageType y);
Vector(StorageType x, StorageType y, StorageType z);
Vector(StorageType x, StorageType y, StorageType z, StorageType w);
...
};
在我的 SWIG 文件中,我想包装一个具有 a Sizeof3和 a StorageTypeof的版本int8_t,所以我这样做了
%module Vector
%{
#include "Vector.h"
%}
%include "stdint.i"
%include "Vector.h"
%ignore Vector(int8_t, int8_t);
%ignore Vector(int8_t, int8_t, int8_t, int8_t);
%template(Vector3DInt8) PolyVox::Vector<3,int8_t>;
但它失败%ignore了请求的构造函数。
似乎 SWIG 在%template宏内部自动从模板参数中“剥离”typedef,因此%template(Vector3DInt8) PolyVox::Vector<3,int8_t>;实际上被转换为%template(Vector3DInt8) PolyVox::Vector<3,unsigned char>;. 因此,%ignore不匹配,因为unsigned char不匹配int8_t。
如果我static_assert()在其中一个我想被忽略的函数中添加一个,我会得到:
source/Vector.inl: In constructor ‘Vector<Size, StorageType>::Vector(StorageType, StorageType) [with unsigned int Size = 3u, StorageType = signed char]’:
build/PolyVoxCorePYTHON_wrap.cxx:6446:100: instantiated from here
source/Vector.inl:56:3: error: static assertion failed: "This constructor should only be used for vectors with two elements."
我也尝试过使用-notemplatereduce,但似乎没有效果。
有没有办法让 SWIG 正确忽略不需要的构造函数?
编辑:我使用 GCC 4.5 和 SWIG 2.0.8
编辑 2:添加stdint.i到.i文件中,因为 Python 类型映射需要它。如果没有stdint.i,则%ignore可以正常工作,但需要在 Python 中实际使用绑定。