缺少分号:C++ 或 SWIG 问题?

Missing semicolon: C++ or SWIG issue?

这个问题实际上是关于 SWIG 的,而不是缺少分号的基本 C++。

我在class(在头文件中)中有以下方法:

class BarClass 
{
    // ... more code goes here
    unsigned int foo(unsigned int val) throw(std::invalid_argument) override;
    // ... more code goes here
};

我有一个 SWIG 接口声明,格式为:

%include "stdint.i"
%include "std_except.i"
%include "exception.i"

%module mymodule
%{
    #include "headerFile.h"
%}
%include "headerFile.h"

该代码用作 C++ 静态库,但也通过 SWIG 公开给 python。使用 GCC / Clang 的正常编译效果很好。

但是,当用 SWIG 包装库时,我收到一个错误:

headerFile.h:22: Error: Syntax error - possibly a missing semicolon.

我可以将方法声明替换为:

unsigned int foo(unsigned int val) throw(std::invalid_argument);

删除覆盖时,SWIG 似乎可以工作,但我收到警告。我的印象是 SWIG 同时对 throw 和 override 的组合感到困惑。

这是 SWIG 错误还是我遗漏的一些愚蠢的东西?

注意: 我非常清楚不推荐使用 throw 声明,但这是 SWIG 获取有关异常的信息并为 Python 生成适当代码的方式。也许在 SWIG 中有 better/newer 方法可以做到这一点?

您的 SWIG 使用的是 C++11:

http://www.swig.org/Doc3.0/CPlusPlus11.html

7.2.11 显式覆盖和 final:特殊标识符 final 和 override 可用于方法和析构函数,例如以下示例:

  virtual void ef() final override;
  virtual ~DerivedStruct() override;

您可以:

  1. 通过 C++ 编译器使用 C++11(例如:-std=c++11
  2. 禁用 SWIG 的 C++11 代码生成器。
  3. 使用技巧#define override