Search code examples
c++android-ndkswiggcc-warning

What is Warning 503 by SWIG?


Please explain what are these warning by SWIG and how to avoid it?

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.

The warnings are generated when SWIG generated C++ code is compiled under Android NDK.


Solution

  • Java doesn't have an equivalent of operator() or operator= in the same sense as C++, so there's no way for SWIG to directly wrap it. Because they might be important you're shown a warning that explains they're not being wrapped. (Missing operator= might be particularly bad sometimes).

    This code exhibits such a warning when running swig -Wall -c++ -java:

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

    But you can silence the warning and tell SWIG to expose the operator directly as a regular member function by saying something like:

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

    Which results in a function called something_else being added in place of operator() in the generated wrapper.

    Or you can assert to SWIG that ignoring those is just fine using:

    %ignore operator()
    

    (You can also apply either of those directives less broadly by qualifying the operators with the class names).