【发布时间】:2014-03-17 22:24:31
【问题描述】:
我对 SWIG 中标准 c++ STL 容器的 SWIG 模板有疑问。我在 Windows (MSVC 2012) 和 Mac、SWIG 2.0.12 和 SWIG 3.0.0 上尝试过。我已将其范围缩小到以下 swig 接口文件,不需要 .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),为什么它会起作用?
【问题讨论】:
标签: c++ templates containers swig