swig和c++ 11的兼容性

swig and C++11 compatibility

本文关键字:兼容性 c++ swig      更新时间:2023-10-16

我正在尝试为一个复杂的项目创建一个python模块,该项目使用了c++ 11中的一些新功能(https://code.google.com/p/cpp-array/)。我的接口文件很简单:

%module array
%{
#include "array-config.hpp"
#include "array.hpp"
%}
%include "array-config.hpp"
%include "array.hpp"

但是当我运行swig时,我得到了很多错误。

第一个错误是constexpr
constexpr static int dim()
{ return d; }

因此,如果我删除constexpr,我可以绕过这个错误。根据http://www.swig.org/Doc3.0/CPlusPlus11.html中的文档,处理这个关键字。

第二个是当找到这个函数时:

template <int d, typename U, typename... Args>
typename std::enable_if<std::is_integral<U>::value and !std::is_pointer<U>::value and d < k, void>::type
init(U i, Args&&... args) {
  assert(i != 0); // Array dimension cannot be zero
  n_[d] = i;
  init<d+1>(args...);
}

我只是想用swig"为真正懒惰的人",但我开始怀疑这在这一点上是否可能。我是否必须复制整个头文件并开始进行调整才能使此工作?因为我刚开始喝酒,我希望你能给我一些建议。

您可以将任何SWIG不喜欢的内容包装在#ifdef

例如,我正在使用我们使用的SWIG版本不支持的c++ 11块。所以我这样做:

namespace glue {
    class GlueServer {
    public:
        virtual void handle(std::string path, GlueHandlerFunc handler, void* arg) = 0;    
#ifndef SWIG
        virtual void handleBlock(std::string path, void (^b)(GlueRequest* req)) = 0;
#endif
// ...