SWIG编译报错:SwigValueWrapper无法转换为std::function的解决方法
解决SWIG处理std::function回调时的类型转换错误
- 需求场景:要实现一个以
std::function形式传递给方法的C++回调,采用std_function.i工具处理该场景 - 编写的.i文件代码:
%include "std_function.i" %std_function(OnStart, void, int64_t);
- 遇到的编译问题:代码生成正常,但在Android Studio编译时触发错误:
error: no viable conversion from 'SwigValueWrapper<std::function<void (long long)>>' to 'base::Util::OnStart' (aka 'function<void (long)>')
问题细节:查看SWIG生成的代码发现,生成了
SwigValueWrapper对象,赋值环节正常,但调用(arg1)->set_callback(arg2)时因类型无法转换失败无效尝试:单独使用
%feature("novaluewrapper")配置,未能解决该类型转换问题有效解决方案:在
struct function<Ret(__VA_ARGS__)>的定义上方添加如下配置:
%feature("novaluewrapper") function<Ret(__VA_ARGS__)>;
内容的提问来源于stack exchange,提问作者David Franco
相关产品推荐
相关产品推荐

