在swig接口中忽略operator<<的重定义
我有两个版本的 operator<<
,它们在不同的命名空间里,但签名是一样的。因为 swig
把它们合并成一个命名空间,所以它们之间发生了冲突,导致我无法运行这个接口。
我不需要在脚本语言(Python)中使用这个流插入操作符,有没有办法让它们不显示出来呢?
我试过 %ignore
指令,但似乎没有什么帮助。
这是一个最小的测试设置。
头文件
//file:test.h
#include <iostream>
#include <boost/numeric/ublas/vector.hpp>
namespace probabilities{
typedef boost::numeric::ublas::vector< double > UnivariateTable;
inline
std::ostream& operator<<( std::ostream& ostr, const UnivariateTable& table){
ostr<<"I am a table";
return ostr;
}
}
namespace positions{
typedef boost::numeric::ublas::vector< double > PositionVector;
inline
std::ostream& operator<<(std::ostream& ostr, const PositionVector& vect){
ostr<<"I am a vector";
return ostr;
}
}
Swig 接口文件
//file:test.i
%module test
%{
#include "test.h"
%}
%ignore operator<<;
%include "test.h"
结果
[dmcnamara]$ swig -c++ -python -I/opt/vista_deps/include -I/opt/vista/include test.i
test.h:26: Error: '__lshift__' is multiply defined in the generated target language module in scope .
test.h:15: Error: Previous declaration of '__lshift__'
2 个回答
1
你还可以使用一些预处理工具:
//file:test.h
#include <iostream>
#include <boost/numeric/ublas/vector.hpp>
namespace probabilities{
typedef boost::numeric::ublas::vector< double > UnivariateTable;
#ifndef SWIG
inline
std::ostream& operator<<( std::ostream& ostr, const UnivariateTable& table){
ostr<<"I am a table";
return ostr;
}
#endif
}
namespace positions{
typedef boost::numeric::ublas::vector< double > PositionVector;
#ifndef SWIG
inline
std::ostream& operator<<(std::ostream& ostr, const PositionVector& vect){
ostr<<"I am a vector";
return ostr;
}
#endif
}
3
在写这个问题的过程中,我突然明白了答案:
你需要为 %ignore
指令指定命名空间:
%ignore positions::operator<<
%ignore probabilities::operator<<