javaout typemap 不适用于 std::vector

javaout typemap doesn't work for std::vector

当我使用 int 时一切正常 -

%immutable S::field;

%typemap(javaout) int S::field {
  //custom code
}

struct S {
  int field;
};

但该方法不适用于 std::vector<int>

%include <std_vector.i>

%immutable S::field;
%template(vector_int) std::vector<int>;

%typemap(javaout) std::vector<int> S::field {
  //custom code
}

struct S {
  std::vector<int> field;
};

示例是使用 swig -java -c++ -module sample sample.i

编译的

请参阅 structure data members 上的 Swig 文档。

重点是:

When a structure member is wrapped, it is handled as a pointer, unless the %naturalvar directive is used where it is handled more like a C++ reference.

所以你需要定义的typemap是

%typemap(javaout) std::vector<int>* S::field {
    //custom code
}

(如果您使用 %naturalvar,则为 %typemap(javaout) std::vector<int>& S::field)。