【发布时间】:2014-09-17 02:18:42
【问题描述】:
我有一个带有可变数量参数的 c++ 函数。
char const* Fun(int num, ...)
{
....//does some processing on the arguments passed
}
用于公开此函数的 Boost Python 代码编写为,
using namespace boost::python;
BOOST_PYTHON_MODULE( lib_boost )
{
def( "Fun", Fun );
}
编译此代码时出现以下错误
在 /boost_1_42_0/boost/python/data_members.hpp:15 包含的文件中, 来自 /boost_1_42_0/boost/python/class.hpp:17, 来自 /boost_1_42_0/boost/python.hpp:18, 来自 Lib_boost.h:3, 来自 Lib_boost.cpp:1: /boost_1_42_0/boost/python/make_function.hpp: 在函数中 'boost::python::api::object boost::python::make_function(F) [with F = const char* ()(int, ...)]': /boost_1_42_0/boost/python/def.hpp:82:
从 'boost::python::api::object 实例化 boost::python::detail::make_function1(T, ...) [with T = const char ()(int, ...)]' /boost_1_42_0/boost/python/def.hpp:91: 实例化 来自 'void boost::python::def(const char, Fn) [with Fn = const char* ()(int, ...)]' Lib_boost.cpp:540: 从这里实例化 /boost_1_42_0/boost/python/make_function.hpp:104:错误:无效 从 'const char ()(int, ...)' 转换为 'const char ()(int) /boost_1_42_0/boost/python/make_function.hpp:104: 错误:
初始化 'boost::mpl::vector2 的参数 1 boost::python::detail::get_signature(RT ()(T0), void*) [with RT = const char*, T0 = int]'
从上面的错误信息中我的理解是 boost python 无法识别带变量参数的函数(从 'const char* ()(int, ...)' 到 'const char 的无效转换) (*)(int)')
公开具有固定/已知参数集的函数与采用可变参数的函数不同。 如何公开具有可变参数的函数?
【问题讨论】:
-
可变参数有哪些类型?它们是否保证是一种单一类型,或者它们可以是异构的?为了使用它们,您如何知道它们是什么类型?
-
第一个参数是整数,其余是异构的(参数的数量也可以变化)
-
那么你怎么知道如何使用它们呢?请注意, printf() 接受可变参数,但也接受格式字符串,它需要知道如何处理参数。没有这样的东西,你可能会过得很糟糕。
-
其实我们是在现有的应用程序n中添加boost接口,函数如下 ` char const* Fun(int num, ...) { ....//参数被va_List捕获 va_list vl; va_start(vl,num); //读取 vl va_end(vl); }` 共享链接中提供的示例,函数参数是固定的,类型也是固定的。
-
建议我为第一个参数是整数后跟'n'个字符串(char *)的情况提供解决方案,其中n从1到20不等
标签: python c++ boost boost-python variadic-functions