用swig包装专门的c++模板类
wrapping specialised c++ template class with swig
考虑以下类声明:
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> >;
}
这两个类之间的区别在于后向向量容器和value()方法的differential签名的特殊化,其中第一个不带参数,而第二个带一个期望的整数。
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了解哪种功能是正确的吗?
欢呼
您可以通过执行以下操作来实现您想要的结果:
%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>;
}
这之所以有效,是因为您在接口文件中编写的不是C++,重要的是SWIG生成了正确的代码。如果你想重复这些,你可以写宏(无论如何,这接近于%template
)。
尽管如此,这并不是一个非常干净的解决方案——我预计这将"只适用于"专业领域,我也看不到更简单的解决方案。
相关文章:
- Python 3.7 和 excess_args 的 SWIG 问题
- 从具有默认值的部分指定模板类继承时发生SWIG错误,具有不带默认值的正向声明
- 为重写std::exception的库生成swig接口时出错
- 元组由 Swig 生成的 Python 包装器返回,用于C++向量
- 无法使用 SWIG 在 Python 中实例化C++类(获取属性错误)
- SWIG Java 在使用 -DSWIGWORDSIZE64 时将int64_t转换为 jlong
- 使用 SWIG 更改生成的 CS 函数中的返回类型
- 在模板化函数中重新定义(忽略)的 SWIG 标识符
- 使用 swig 追加到字节数组
- 如何使用 swig 修改类构造函数以保留对其中一个构造函数参数的引用?
- Java SWIG wrapper vs direct function calling
- <string> 如何在使用 SWIG 时将 int 数组和 List 作为参数传递给 C# C++
- 类型为 'std::map< char,int > 的 <Swig 对象的代理
- 使用SWIG从C++回调到C#
- 从 C++ 到 Python 的 SWIG:未定义的符号导入问题
- 如何使用 swig C++命名空间作为 python 模块公开
- 分解SWIG Python接口 - 容器会产生命名空间冲突
- 在自定义类型图中重用 SWIG 映射
- 使用 Swig 在成员中包装具有互斥锁的 C++ 类时出现问题
- 如何制作一个满足SWIG中接口的python类