c++ - C++ STL 容器(浮点 vector )的 SWIG 模板类型特征错误

标签 c++ templates containers swig

我对 SWIG 中标准 c++ STL 容器的 SWIG 模板有疑问。我试过 Windows (MSVC 2012) 和 Mac、SWIG 2.0.12 和 SWIG 3.0.0。我已将其缩小到以下 swig 接口(interface)文件,不需要 .cxx/.h。我假设我做错了什么,但是什么?

%module example

%{
#include <string>
#include <map>
#include <vector>
%}

%include std_string.i
%include std_map.i
%include std_vector.i

%template (value_map) std::map<std::string, std::vector<float> >;
%template (float_vector) std::vector<float>;

当我尝试使用

构建它时
swig -python -c++ foo.i
g++ -o foo_wrap.os -c -fPIC \
 -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 \
 foo_wrap.cc

我收到以下模板错误。如果我删除任何 template 行,它编译正常(但我的真实应用程序当然不会工作;我需要那些模板)。

foo_wrap.cc:3465:67: error: no member named 'type_name' in 'swig::traits<float>'
    return traits<typename noconst_traits<Type >::noconst_type >::type_name();
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
foo_wrap.cc:3475:48: note: in instantiation of function template specialization 'swig::type_name<float>' requested here
      static swig_type_info *info = type_query(type_name<Type>());
                                               ^
foo_wrap.cc:3482:31: note: in instantiation of member function 'swig::traits_info<float>::type_info' requested here
    return traits_info<Type>::type_info();
                              ^
foo_wrap.cc:3516:46: note: in instantiation of function template specialization 'swig::type_info<float>' requested here
      return SWIG_InternalNewPointerObj(val, type_info<Type>(), owner);
                                             ^
foo_wrap.cc:1174:91: note: expanded from macro 'SWIG_InternalNewPointerObj'
#define SWIG_InternalNewPointerObj(ptr, type, flags)    SWIG_Python_NewPointerObj(NULL, ptr, type, flags)
                                                                                             ^
foo_wrap.cc:3522:37: note: in instantiation of member function 'swig::traits_from_ptr<float>::from' requested here
      return traits_from_ptr<Type>::from(new Type(val), 1);
                                    ^
foo_wrap.cc:3541:31: note: in instantiation of member function 'swig::traits_from<float>::from' requested here
    return traits_from<Type>::from(val);
                              ^
foo_wrap.cc:4094:14: note: in instantiation of function template specialization 'swig::from<float>' requested here
      return swig::from(v);
             ^
foo_wrap.cc:4162:9: note: in instantiation of member function 'swig::from_oper<float>::operator()' requested here
        return from(static_cast<const value_type&>(*(base::current)));
               ^
foo_wrap.cc:4204:16: note: in instantiation of member function 'swig::SwigPyIteratorClosed_T<__gnu_cxx::__normal_iterator<float *, std::vector<fl\
oat, std::allocator<float> > >, float, swig::from_oper<float> >::value' requested here
    return new SwigPyIteratorClosed_T<OutIter>(current, begin, end, seq);
               ^
foo_wrap.cc:5153:14: note: in instantiation of function template specialization 'swig::make_output_iterator<__gnu_cxx::__normal_iterator<float *,\
 std::vector<float, std::allocator<float> > > >' requested here
      return swig::make_output_iterator(self->begin(), self->begin(), self->end(), *PYTHON_SELF);
             ^

我注意到 SWIG 没有为 float 生成 swig::traits 的特化。但是,如果我注释掉其中一个模板(value_map 或 float_vector),为什么它会起作用?

最佳答案

奇怪。一些尝试:

  • 交换两个模板行,使 float_vector 在 value_map 之前
  • 从 map 中删除 vector ,看看是否可行:

    %template (float_vector) std::vector<float>;
    %template (value_map) std::map<std::string, float>;
    
  • 在 map 中使用 float_vector:

    %template (float_vector) std::vector<float>;
    %template (value_map) std::map<std::string, float_vector >;
    

关于c++ - C++ STL 容器(浮点 vector )的 SWIG 模板类型特征错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22466717/

相关文章:

c++ - 编写二次公式程序时的编译错误

c++ - 使用 C++ 模板类的简单矩阵示例

C++模板和迭代器

docker - Spring Batch是否可以使用外部应用程序启动容器?

c++ - Opengl Rgba 从 1 个分量到灰度

c++ - 异步运行的函数的返回类型应该是什么

c++ - 打开路径名中带有波浪号 (~) 的 ofstream

c++ - 什么时候显式实例化模板是合适的?

C++ 映射、 vector 、带引用的对

.net - .NET 中的双向映射