考虑以下类声明:
namespace X {
template<class T>
class Foo {
public:
Foo();
virtual ~Foo();
T value() const;
};
template<class T>
class Foo<vector<T>> {
public:
Foo();
virtual ~Foo();
T value( const int ) const;
};
}
对于他们,我在 foo.i 文件中有以下声明
%include "stl.i"
%include "std_string.i"
%include "std_vector.i"
namespace X {
using namespace std;
template<class T>
class Foo {
public:
Foo();
virtual ~Foo();
T value() const;
};
template<class T>
class Foo<vector<T> > {
public:
Foo();
virtual ~Foo();
T value( const int ) const;
};
%template(FooInt) Foo<int>;
%template(FooString) Foo<string>;
%template(FooVectorInt) Foo<vector<int> >;
}
这两个类之间的区别在于后者专门化为 vector 容器和 value() 方法的不同签名,其中第一个不接受任何参数,而第二个接受一个期望整数。
swig 将 %template(FooVectorInt)
放在一起的包装代码是错误的,因为它调用了 value()
方法而不是专用 vector 方法 值(常量整数)
。给我以下编译错误消息:
foo_wrap.cxx: in function »int _wrap_FooVectorInt_value(lua_State*)«:
/home/noobsaibot/foo/bindings/src/lua/foo_wrap.cxx:6706:81: error: no matching function to call »X::Foo<std::vector<int> >::value() const«
/home/noobsaibot/foo/src/foo.h:78:5: note: candidate is: T X::Foo<std::vector<_RealType> >::value(int) const [with T = int, int = unsigned int]
关于我可能遗漏了什么以使 swig 了解哪个函数是正确的,有什么建议吗?
干杯
最佳答案
您可以通过以下方式实现您想要的结果:
%include "stl.i"
%include "std_string.i"
%include "std_vector.i"
namespace X {
using namespace std;
%rename(FooVectorInt) Foo<std::vector<int> >;
class Foo<std::vector<int> > {
public:
virtual ~Foo();
int value( const int ) const;
};
template<class T>
class Foo {
public:
Foo();
virtual ~Foo();
T value() const;
};
%template(FooInt) Foo<int>;
%template(FooString) Foo<string>;
}
之所以可行,是因为您在接口(interface)文件中编写的不是 C++,重要的是正确的代码是由 SWIG 生成的。如果你想重复这些,你可以写宏(这接近于 %template
无论如何)。
这仍然不是一个非常干净的解决方案 - 我希望它“只适用于”特化,而且我也看不到更简单的解决方法。
关于c++ - 用 swig 包装专门的 c++ 模板类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9757642/