gpt4 book ai didi

python - 禁止通过 swig 为 python-C++ 接口(interface)生成隐式类型检查代码

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:08:04 27 4
gpt4 key购买 nike

对于下面的简单函数:

#include <iostream>
void Echo(int no) {
std::cout << "no: " << no << std::endl;
}

我有以下 swig 接口(interface)文件:

%module example
%{
#include "example.h"
%}
%include "example.h"

我可以使用 swig 生成包装器并将其测试为:

from example import Echo
import numpy as np

no = 2
Echo( np.int(no) ) # OK
Echo( np.int32(no) ) # Error
Echo( np.int64(no) ) # Error

swig 生成类型检查包装器代码,这会导致第二次和第三次调用出错。这很好,但是有什么方法可以覆盖/禁用为合法的类型转换生成这种类型检查代码吗?

最佳答案

这可以通过在生成包装器时将 castmodenocastmode 选项传递给 swig 来控制。默认为 nocastmode。这不需要定义任何 typemaps 并且也独立于 python2/python3。

示例:要允许上面示例中的类型转换,可以将包装器生成为:

swig -c++ -python -castmode example.i

关于python - 禁止通过 swig 为 python-C++ 接口(interface)生成隐式类型检查代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46523866/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com