我可以从函数指针中提取函数参数吗?
我可以从函数指针中提取额外的信息(例如参数数量及其类型)吗?如果(很可能)不是,允许程序员传递函数指针和可变数量的参数 编辑:我的意思是它应该允许具有不同数量的函数的最佳方式是什么确保程序员不会传递错误的参数类型时所采用的参数
。
我想做的事情: 将函数及其参数公开给脚本语言,而无需程序员手动输入参数(因此,当程序员传递函数指针和函数接受的不正确参数时,可能会发生错误)。
更多说明:我应该能够输出函数名称和参数类型以用于文档目的。
----------------- 解决方案 -----------------
詹姆斯的答案就是我一直在寻找的。这是代码,因为它有点复杂,应该可以帮助陷入相同情况的任何人。
boost::function_traits<boost::remove_pointer
<BOOST_TYPEOF(&SomeFunction)>::type>::arg2_type);
如果需要输出,请使用 RTTI(假设您在编译器中启用了 RTTI)
std::cout << typeid(boost::function_traits<boost::remove_pointer
<BOOST_TYPEOF(&SomeFunction)>::type>::arg2_type).name() << std::endl;
Can I extract extra information (like number of arguments and their types) from a function pointer? If (most likely) not, what is the best way to allow a programmer to pass in a function pointer and the variable number of arguments EDIT: I meant that it should allow functions with different number of arguments
that it takes while making sure that the programmer does not pass incorrect argument types.
What I want to do:
Expose functions and its arguments to scripting language without having a programmer to type the arguments out manually (and therefore allowing mistakes to occur where the programmer passes the function pointer and the incorrect arguments that the function takes in).
A little more clarification: I should be able to output the function name and the argument types for documentation purposes.
----------------- SOLUTION -----------------
James's answer is what I was looking for. Here is the code as it is a bit convoluted that should help anybody stuck in the same situation.
boost::function_traits<boost::remove_pointer
<BOOST_TYPEOF(&SomeFunction)>::type>::arg2_type);
If you require output, use RTTI (assuming you have RTTI enabled in compiler)
std::cout << typeid(boost::function_traits<boost::remove_pointer
<BOOST_TYPEOF(&SomeFunction)>::type>::arg2_type).name() << std::endl;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
function_traits
来自 Boost.TypeTraits 库。为了完全实现您所描述的具有类型安全的有效可变参数函数的场景,您确实需要可变参数模板,这是 C++0x 中添加的一项新功能,并受到一些最近发布的编译器(例如 Clang、gcc)的支持。如果您需要更广泛的东西,您可能需要实现自己的运行时类型系统。
You can get the arity, return type, and parameter types of a function using
function_traits
from the Boost.TypeTraits library.To fully enable your described scenario of having what is effectively a variadic function that is type safe, you really need variadic templates, a new feature being added in C++0x and supported by some recently released compilers (e.g., Clang, gcc). If you need something more extensive, you will probably need to implement your own runtime type system.
我不太清楚你在追求什么,但是boost。function_types 可以告诉你函数指针的返回类型、参数数量和参数类型。
I'm not entirely clear what you're after, but boost.function_types can tell you the return type, number of arguments, and argument types of a function pointer.
您可能需要考虑使用 Boost::Function库和 Boost::TypeTraits< /a>.我发现 Boost::Function 使函数指针更容易处理,并允许您以通用方式使用函数对象(即您不受函数对象类型的限制,这是一个痛苦),这比原始函数指针
You may want to consider using the Boost::Function library and Boost::TypeTraits. I find Boost::Function makes function pointers easier to deal with and allows you to use function objects in a generic way (i.e. you are not constrained by the type of the function object which is a pain), which are all round much nicer than raw function pointers
您可以将 doxygen xml 输出 转换为适合脚本编写的正确声明/元数据语言。 这个其他问题中的信息可能会帮助您入门。否则 SWIG 可能会帮助你,它是为生成跨语言包装器而设计的,并且可能会生成与你的兼容的包装器选择的脚本语言。
You could probably convert doxygen xml output into the proper declarations/metadata for your scripting language. The information in this other question might get you started. Otherwise SWIG might help you, it's designed for generating cross-language wrappers, and can probably generate them compatible with your scripting language of choice.