SWIG and C++14

# 8 SWIG and C++14

- Introduction
- Core language changes
  - Binary integer literals
  - Return type deduction
- Standard library changes

## 8.1 Introduction

This chapter gives you a brief overview about the SWIG implementation of the C++14 standard. There isn't much in C++14 that affects SWIG, however, work has only just begun on adding C++14 support.

Compatibility note: SWIG-4.0.0 is the first version to support any C++14 features.

## 8.2 Core language changes

### 8.2.1 Binary integer literals

C++14 added binary integer literals and SWIG supports these. Example:

    int b = 0b101011;

### 8.2.2 Return type deduction

C++14 added the ability to specify `auto` for the return type of a function and have the compiler deduce it from the body of the function (in C++11 you had to explicitly specify a trailing return type if you used `auto` for the return type).

SWIG parses these types of functions, but with one significant limitation: SWIG can't actually deduce the return type! If you want to wrap such a function you will need to tell SWIG the return type explicitly.

The trick for specifying the return type is to use `%ignore` to tell SWIG to ignore the function with the deduced return type, but first provide SWIG with an alternative declaration of the function with an explicit return type. The generated wrapper will wrap this alternative declaration, and the call in the wrapper to the function will call the actual declaration. Here is an actual example:

    std::tuple<int, int> va_static_cast();
    %ignore va_static_cast();
    #pragma SWIG nowarn=SWIGWARN_CPP14_AUTO

    %inline %{
    #include <tuple>

    auto va_static_cast() {
        return std::make_tuple(0, 0);
    }
    %}

For member methods the trick is to use `%extend` to redeclare the method and call it as follows:

    %extend X {
      const char * a() const { return $self->a(); }
    }
    %inline %{
    struct X {
      auto a() const {
        return "a string";
      }
    };
    %}

Compatibility note: SWIG-4.2.0 first introduced support for functions declared with an auto return without a trailing return type.

## 8.3 Standard library changes