所以我尝试创建 std_complex.i (因为在 C# SWIG 库中没有这样的开箱即用)。这是我尝试过的:
%{
#include <complex>
%}
namespace std {
template<class T>
class complex {
public:
complex(T r, T i);
complex(const complex<T> & other);
T imag();
T real();
%rename(Add) operator+=;
%rename(Substract) operator-=;
%rename(Multiply) operator*=;
%rename(Divide) operator/=;
complex<T>& operator+= (const complex<T>& rhs);
complex<T>& operator-= (const complex<T>& rhs);
complex<T>& operator*= (const complex<T>& rhs);
complex<T>& operator/= (const complex<T>& rhs);
};
}
%template(Complex) std::complex<double>;
生成的包装器确实执行类成员操作,但我不知道如何转发运算符的非成员函数,std::complex
例如==
,或+
。那么如何在 SWIG C# 中转发非成员函数呢?
例如这个operator+
:
template<class T> complex<T> operator+(const complex<T>& lhs, const complex<T>& rhs);
template<class T> complex<T> operator+(const complex<T>& lhs, const T& val);
template<class T> complex<T> operator+(const T& val, const complex<T>& rhs);
?