SWIG解析器错误

时间:2015-05-22 13:06:16

标签: swig

我有以下头文件。

#include <string>

namespace A {
namespace B {

    struct Msg {
        std::string id;
        std::string msg;

        Msg(std::string new_id, std::string new_msg)
        : id(new_id), msg(new_msg)
        {
        }
    };

    template<bool HAS_ID>
    class ID {
    public:
        template<typename TOBJ>
        auto get(TOBJ parent) -> decltype(parent.id()) {
            return parent.id();
        }
    };   
} // namespace B
} // namespace A

当我把它打开时,它给了我一个错误

Error: Syntax error in input(3).在第20行指向

auto get(TOBJ parent) -> decltype(parent.id())

目标语言是Java

我该如何解决这个问题?我只想为Msg结构创建包装器,而不是在头文件中创建其他内容。由于这看起来像Swig解析器错误,使用%ignore指令似乎不起作用。

谢谢

1 个答案:

答案 0 :(得分:3)

虽然SWIG 3.x添加了有限的decltype支持,但您现在的情况似乎不受支持。 (见decltype limitations

我认为您现在最好的做法是将预处理器宏中的违规代码包围起来以隐藏它,例如:

#include <string>

namespace A {
namespace B {

    struct Msg {
        std::string id;
        std::string msg;

        Msg(std::string new_id, std::string new_msg)
        : id(new_id), msg(new_msg)
        {
        }
    };

    template<bool HAS_ID>
    class ID {
    public:
#ifndef SWIG
        template<typename TOBJ>
        auto get(TOBJ parent) -> decltype(parent.id()) {
            return parent.id();
        }
#endif
    };   
} // namespace B
} // namespace A

如果由于某种原因无法编辑文件,则有两种选择:

  1. 不要将%include与未解析的头文件一起使用。而是写一些像:

    %{
    #include "header.h" // Not parsed by SWIG here though
    %}
    
    namespace A {
    namespace B {
    
        struct Msg {
            std::string id;
            std::string msg;
    
            Msg(std::string new_id, std::string new_msg)
            : id(new_id), msg(new_msg)
            {
            }
        };
    
    } // namespace B
    } // namespace A
    
    in your .i file, which simply tells SWIG about the type you want to wrap and glosses over the one that doesn't work.
    
  2. 或者使用预处理器获得创意并找到一种方法来使用bodge隐藏它,在.i文件中你可以编写如下内容:

    #define auto // \
    void ignore_me();
    
    %ignore ignore_me;
    

    另一个类似的提议是隐藏decltype的内容:

    #define decltype(x) void*
    

    这只是告诉SWIG假设所有decltype用法都是一个void指针。 (需要SWIG 3.x并且可以与%ignore结合使用,或者使用类型图来实现修复)