考虑以下类声明:
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> >;
}
这两个类之间的区别在于后者对向量容器的特殊化andvalue() 方法的不同签名,其中第一个方法不带参数,而第二个方法需要一个整数。
由 swig 组合在一起的包装器代码包装了%template(FooVectorInt)
错误的,因为它调用value()
方法代替专门的向量方法value(const int)
。给我以下编译错误消息:
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 理解哪个函数是正确的函数,我可能会缺少什么建议?
cheers
您可以通过执行以下操作来实现您想要的结果:
%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>;
}
这是有效的,因为你在接口文件中写入的内容isn'tC++ 重要的是 SWIG 生成正确的代码。如果你想重复这么多,你可以编写宏(这接近于%template
无论如何)。
但这仍然不是一个非常干净的解决方案 - 我希望这能够“仅适用于”专业化,而且我也看不到更简单的解决方法。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)