SWIG 解析器错误 [英] SWIG parser error

查看:53
本文介绍了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

当我喝它时,它给了我一个错误

When i swig it, it gives me an error

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

Error: Syntax error in input(3). at line 20 pointing to line

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

目标语言是 Java

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

How can i fix this problem? I only want to create wrapper for Msg struct and for nothing else in the header. As this looks like a Swig parser error, using %ignore directive does not seem to work.

谢谢

推荐答案

虽然 SWIG 3.x 添加了有限的 decltype 支持,但看起来您的情况目前不受支持.(请参阅decltype 限制)

Although SWIG 3.x added limited decltype support it looks like the case you have is unsupported currently. (See decltype limitations)

我认为你现在最好的方法是在预处理器宏中包围有问题的代码以隐藏它,例如:

I think the best you'll get for now is to surround the offending code in preprocessor macros to hide it, e.g.:

#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

<小时>

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


If you can't edit the file like that for whatever reason there are two options:

  1. 不要将 %include 与不解析的头文件一起使用.而是写一些类似的东西:

  1. Don't use %include with the header file that doesn't parse. Instead write something like:

%{
#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.

  • 或者通过预处理器获得创意并找到一种使用 bodge 隐藏它的方法,在您的 .i 文件中,您可以编写如下内容:

  • Alternatively get creative with the pre-processor and find a way to hide it using a bodge, inside your .i file you could write something like:

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

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

    Another similar bodge would be to hide the contents of decltype with:

    #define decltype(x) void*
    

    这只是告诉 SWIG 假设所有 decltype 用法都是空指针.(需要 SWIG 3.x 并且可以与 %ignore 结合使用,这应该可以忽略,或者使用 typemap 来真正修复它)

    Which just tells SWIG to assume all decltype usage is a void pointer. (Needs SWIG 3.x and could be combined with %ignore which ought to do the ignore, or a typemap to really fix it)

    这篇关于SWIG 解析器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆