Search code examples
c++c++11swig

Missing semicolon: C++ or SWIG issue?


This question is actually about SWIG, not a basic C++ missing a semicolon.

I have the following method in a class (in the header file):

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

I have a SWIG interface declaration in the form:

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

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

The code is used as a C++ static library but also exposed to python via SWIG. Normal compilation with GCC / Clang works well.

However, when wrapping the library with SWIG, I receive an error:

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

I could replace the method declaration with:

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

When removing the override, SWIG seems to work but I get warnings. I have the impression that SWIG gets confused by the combination of throw and override at the same time.

Is this is SWIG bug or something silly that I am missing?

Note: I very much aware that using throw declarations is deprecated but that is the way SWIG gets information about exceptions and generates the appropriate code for Python. Maybe there is a better/newer way to do this in SWIG?


Solution

  • Your are using SWIG with C++11:

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

    7.2.11 Explicit overrides and final: The special identifiers final and override can be used on methods and destructors, such as in the following example:

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

    You may either:

    1. Use C++11 with the C++ compiler (eg: -std=c++11). Recommended
    2. Disable (remove) C++11 features in code for SWIG's generator.
    3. Use trick #define override to make the keyword override as a NO-OP.