避免在函数调用中计算数组元素
Avoid counting array elements in function call
我正在定义一个函数签名以执行远程过程调用。由于undefined behavior
,我无法在调用表达式中递增索引变量,因此我最终从 0
计数到最后一个索引,并将每个索引作为参数传递给函数。有没有更优雅的方法可以在不计数的情况下完成此操作?我在想一个循环什么的。当固定参数计数更改为例如 16
参数而不是8
.
typedef unsigned long long int functionType(int, int, int, int, int, int, int, int);
unsigned long long int call_address(uintptr_t real_address, const unsigned int *arguments) {
auto function = (functionType *) real_address;
// We count instead of incrementing an index variable because: operation on 'argumentIndex' may be undefined
return function(arguments[0], arguments[1],
arguments[2], arguments[3],
arguments[4], arguments[5],
arguments[6], arguments[7]);
}
我知道有使用 va_start
、va_list
和 va_end
的变量参数,但我不确定它们是否可以在这里使用。
解决方案的一部分涉及从arguments
数组中解压缩固定数量的值并使用它调用function
。以下 C++14 代码将执行此操作:
template <typename F, size_t... Is>
unsigned long long int our_invoke(F f, const unsigned int * args, std::index_sequence<Is...>) {
return f(args[Is]...);
}
unsigned long long int call_address(uintptr_t real_address, const unsigned int *arguments) {
auto function = (functionType *) real_address;
return our_invoke(function, arguments, std::make_index_sequence<8>{});
}
相关文章:
- 如何计算数组中元素的位数?(不是数组的长度),并计算其数字的总和
- 如何在 c++ 中计算相同数组元素的数量
- 我写了这个简单的代码,应该计算一系列数组元素的最高数量,但结果很奇怪
- 如何在 c++ 中计算数组中的元素数量?
- 避免在函数调用中计算数组元素
- 计算数组的不同元素
- 为什么数组元素长度的地址计算可被 2 的幂整除更有效
- 如何在c++中计算数组的元素
- OpenCV函数来计算类似于MATLAB产品的数组元素的乘积?
- 计算字符串/数组元素的十六进制,然后将其转换为十进制
- 计算数组 C++ 中的特定元素
- 计算数组中的元素重复的次数
- 计算数组元素的平均值并显示它们
- 在有效地计算数组中第二个元素较少的对时,我哪里错了
- 使用模板函数计算数组中的元素数量
- 优化了数组元素的计算和设置
- 使用数组元素执行计算
- 计算数组元素中不同绝对值的个数
- 计算数组中的元素
- 根据指向第一个和最后一个元素的指针计算数组中的元素数