如何根据其ARITY(或编译时已知的其他信息)以不同的方式调用函数对象
How to call a function object differently, depending on its arity (or other information known at compile time)?
在功能模板中,我想以函数为单位或函数对象,具体取决于其ARITY(它需要多少参数)。在伪代码:
if arity(f) == 1:
f(x)
if arity(f) == 2:
f(x, y)
if arity(f) == 3:
f(x, y, z)
如何在C ?
中完成此操作 edit 要澄清难度:f(x, y, z)
如果f
仅采用2个参数,则不会编译,反之亦然,f(x, y)
在f
需要3个参数时不会编译。
带有C 11:
#include <iostream>
template <typename F> struct Traits;
template <typename R, typename... A>
struct Traits<R (A...)>
{
static constexpr unsigned Arity = sizeof...(A);
};
void f(int, int, int);
int main() {
std::cout
<< Traits<void()>::Arity
<< Traits<void(int)>::Arity
<< Traits<void(int, int)>::Arity
<< Traits<decltype(f)>::Arity
<< 'n';
return 0;
}
否则,您可以查找boost ::功能:http://www.boost.org/doc/libs/1_55_0b1/doc/html/function.html
相关文章:
- 调试符号中缺少 QObject 类信息(编辑但存在其他 Qt 类)
- 在指针中对其他信息进行编码
- 只知道运行时的数据类型.如何将数据详细信息隐藏到使用它们的其他类
- 针对特殊情况,使用其他状态信息扩展基元类型
- 如何使用所有其他文件信息来编译代码,例如HAAR级联训练的权重的XML文件
- Windows表单如何使用其他函数回调图表信息
- C GTEST在测试结束时仅在测试失败时打印其他信息
- 如何根据其ARITY(或编译时已知的其他信息)以不同的方式调用函数对象
- 基于其他信息的C 排序索引
- 获取有关io_service的其他信息的任何方法
- 将信息传递给 Cocos2D-X 中的其他 CCScene?
- 为什么 Boost 和其他库的某些部分(如 Folly)在详细信息子文件夹中
- 在C++中设置产品ID版本、公司和其他应用程序信息
- 如何从一个文件中提取信息并将信息拆分为其他四个文件
- Internet Explorer 如何通知其他浏览器有关代理设置更改的信息
- 如何在二进制文件(.net或非托管文件)中注入/更新Version和其他详细信息
- 编辑C++/程序集调试器的源代码以包含其他程序集信息
- 什么是最快的方式来获得日期,时间和解析成字符串以及一些其他信息
- 附加信息:试图读取或写入受保护的内存.这通常表明其他内存已损坏
- 搜索一个数据结构,它可以像地图一样工作,也可以检索其他信息