忽略运算符<<在swig界面

Ignore redefinition of operator<< in swig interface

本文关键字:swig 界面 运算符      更新时间:2023-10-16

我在不同的名称空间中有两个版本的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__'

在写问题的过程中,我意识到了答案:

您需要为%ignore指令指定名称空间:

%ignore positions::operator<<
%ignore probabilities::operator<<

您也可以使用预处理工具:

//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
}