什么是SWIG的警告503

What is Warning 503 by SWIG?

本文关键字:警告 SWIG 什么      更新时间:2023-10-16

请解释一下SWIG的这些警告是什么以及如何避免它?

Warning 503: Can't wrap 'operator ()' unless renamed to a valid identifier.
Warning 503: Can't wrap 'operator =' unless renamed to a valid identifier.
Warning 503: Can't wrap 'operator *' unless renamed to a valid identifier.

SWIG 生成的代码C++在 Android NDK 下编译时,会生成警告。

Java没有

与C++相同的operator()operator=,因此SWIG无法直接包装它。因为它们可能很重要,所以会显示一条警告,说明它们没有被包裹。(缺少operator=有时可能特别糟糕)。

此代码在运行swig -Wall -c++ -java时显示如下警告:

%module Sample
struct test {
  bool operator()();
};

但是,您可以静音警告,并告诉 SWIG 将运算符直接公开为常规成员函数,方法是说:

%module Sample
%rename(something_else) operator();
struct test {
  bool operator()();
};

这会导致在生成的包装器中添加一个名为 something_else 的函数来代替operator()

或者你可以向SWIG断言,忽略这些是很好的:

%ignore operator()

(您还可以通过使用类名限定运算符来不太广泛地应用这些指令中的任何一个)。

如果要

在目标语言中使用重载运算符,则需要在 SWIG 中以特殊方式处理重载运算符。看这里。

如果有一个

%rename 指令出现在用于实例化函数的 %template 指令之前,您可能会遇到此警告,如下所示:

%module some_module
%rename("%(undercase)s", %$isfunction) ""; 
// equivalently  %rename("%(utitle)s", %$isfunction) "";
%inline %{
template<class T>
void MyFunction(){}
%}
// ...
%template(MyIntFunction) MyFunction<int>;

警告 503:除非重命名为有效标识符,否则无法包装"my_function"

而且您不能尝试在%template中预测重命名:

%template(MyIntFunction) my_function<int>;

因为那样你会得到

错误:模板"myfunction"未定义。

如果您正在应用全局重命名规则,这非常令人沮丧,并且您实际上只需要"忽略重命名"几件事。与类型映射不同,重命名指令始终有效。能够关闭它们会很好,即使是暂时的。

我想出的唯一解决方法是返回到%rename并将其更新为显式仅匹配(或者更确切地说,不匹配)您内联声明的模板函数。这样:

// don't rename functions starting with "MyFunction"
%rename("%(undercase)s", %$isfunction, notregexmatch$name="^MyFunction") "";

它并不完美,但这是一种解决方法。

(这一切都是在SWIG 4.0中完成的)