Swig:如何将c++字符串const类型映射到python字符串?

2024-09-19 23:37:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个拥有这些原型的构造器:

YCPTerm(const string& s);
YCPTerm(const string& s, const YCPList& l);
YCPTerm(bytecodeistream & str);

我使用swig生成python绑定。在python中,我尝试调用构造函数,得到以下结果:

^{pr2}$

我发现了this answer这似乎是一个类似的问题,但是typemap类型检查对我不起作用。在

这就是我所尝试的:

%typemap(typecheck,precedence=141) const std::string& str {
  $1 = Z_TYPE_PP($input) == IS_STRING;
}

我怎么解决这个问题?我需要输入和输出类型映射来转换值吗?我一直在reading about typemap's here,不知道如何对常量字符串进行类型映射;。在


Tags: answer类型stringthis原型swigstrconst
2条回答

std_string.i库包含在std::string和Python字符串之间来回转换的typedef。有关详细信息,请参见the documentation on the std::string library in SWIG。在

我找到了answer here。在

将这些行添加到我的.i文件中修复了错误:

%include std_string.i
%inline %{
using namespace std;
%}

我不明白为什么。也许其他人能回答这个问题。在

相关问题 更多 >