SWIG parser error

前端 未结 1 1378
孤街浪徒
孤街浪徒 2021-01-24 10:35

I have following header file.

#include 

namespace A {
namespace B {

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

        M         


        
1条回答
  •  南笙
    南笙 (楼主)
    2021-01-24 10:52

    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 
    
    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
        class ID {
        public:
    #ifndef SWIG
            template
            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. 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.
      
    2. 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;
      

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

      #define decltype(x) void*
      

      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)

    0 讨论(0)
提交回复
热议问题