lambda/任意Arity的功能和以捕获为函数参数的功能
Lambda/function of arbitrary arity and with captures as a function argument
在我的C 程序中,我经常需要在其参数的所有可能值中构建函数值的向量,这是在某些小有限字段上的所有可能值。例如,类似的东西:
int q = 7;
vector<int> GFq;
for (int x = 0; x < q; x++) GFq.push_back(x);
auto P = [q](int x, int y) -> int { return (x*x+y) % q; };
auto Q = [q](int x, int y) -> int { return (x+2*y) % q; };
auto f = [q,P,Q](int x1, int y1, int x2, int y2)
-> int {return (P(x1,y1) + Q(x2,y2)) % q; }
vector<int> table;
for (int x1: GFq) for (int y1: GFq) for (int x2: GFq) for (int y2: GFq)
table.push_back(f(x1,y1,x2,y2));
这种模式在我的代码中经常重复,以至于我自然希望使其成为一个函数。所以我需要这样的东西:
template<typename F> // not sure if I need to use templates
vector<int> tabulate(int q, F f) {
// run through values 0..q-1 for all arguments of f
// and store the values of f to the resulting vector
}
一些问题/问题:
- 我希望能够将任意功能传递给
tabulate()
,包括不同的ARITY(即f(x)
,f(x,y)
等)
)
- 我想构建我传递的"飞行"功能,包括其他功能的用法(与第一个代码段中的
P
和Q
构建的f
相同的方法 - 如果我设法传递了这样的功能,如何在
tabulate()
中的所有可能的f
参数(即0..q-1
)运行一个循环?
我希望能够通过任意函数来表(),包括不同的arity(即f(x),f(x,y)等)
)
使tabulate
接受任意类型作为函数的对象。
我想构造我传递的"飞行"功能,包括其他功能的用法(与第一个代码spippet中的p和q构造的f相同的方法
您可以直接将lambda用作函数参数。
如果我设法传递了这样的功能,我如何在fabulate()?
pseudocode:
params = {0, ..., 0};
while (1)
{
// Call function with `params` here.
int i = 0;
for (i = 0; i < params.size(); i++)
{
params[i]++;
if (params[i] == q)
params[i] = 0;
else
break;
}
if (i == params.size())
break;
}
实际上,您需要将参数存储在std::array
(或std::tuple
,如下面的代码所做的那样),然后使用std::apply
使用这些参数调用您的函数。
完整的实现:
#include <cstddef>
#include <iostream>
#include <tuple>
#include <type_traits>
#include <utility>
#include <vector>
template <typename T, typename ...P, std::size_t ...I>
bool increment_tuple_impl(T q, std::tuple<P...> &t, std::index_sequence<I...>)
{
auto lambda = [&](auto index) -> bool
{
auto &elem = std::get<index.value>(t);
elem++;
if (elem == q)
{
elem = 0;
return 0;
}
else
{
return 1;
}
};
return (lambda(std::integral_constant<std::size_t, I>{}) || ...);
}
template <typename T, typename ...P>
bool increment_tuple(T q, std::tuple<P...> &t)
{
return increment_tuple_impl(q, t, std::make_index_sequence<sizeof...(P)>{});
}
template <typename T, typename F, std::size_t MaxArity, typename ...P>
auto tabulate_impl(T q, F &&f)
{
if constexpr (!std::is_invocable_v<F, P...>)
{
static_assert(sizeof...(P) < MaxArity, "Invalid function.");
return tabulate_impl<T, F, MaxArity, P..., T>(q, std::forward<F>(f));
}
else
{
using return_type = std::invoke_result_t<F, P...>;
std::vector<return_type> vec;
std::tuple<P...> params{};
do
{
vec.push_back(std::apply(f, params));
}
while (increment_tuple(q, params));
return vec;
}
}
template <typename T, typename F>
auto tabulate(T q, F &&f)
{
constexpr int max_arity = 8;
return tabulate_impl<T, F, max_arity, T>(q, std::forward<F>(f));
}
int main()
{
auto v = tabulate(3, [](int x, int y){return x*10 + y;});
// Prints `0 10 20 1 11 21 2 12 22`.
for (auto x : v)
std::cout << x << ' ';
}
相关文章:
- 如何在C++中调用默认模板功能参数?
- 为什么指定数据类型而不是构造功能参数?C
- 为什么在相同大小的功能参数中隐式转换不会发出警告
- 无全局变量或功能参数的递归调用
- 将功能参数添加到向量
- 使用ENABLE_IF和SFINAE时,功能参数类型扣除(std容器,例如向量)失败
- C 功能参数中的返回参数
- 将std ::字符串移至功能参数
- 可以在功能参数列表中多次使用函数模板的模板参数名称
- C 元组类型用于功能参数类型
- 功能参数包
- 我可以使用模板功能参数C 更改模板类型
- 为了支持移动语义,应通过unique_ptr,值或rvalue获取功能参数
- 创建模板函数以使用该功能参数调用其他函数
- C 功能参数扩展了多个类
- GDB CoreFile不查看功能参数
- 功能参数导致堆栈溢出
- C 在功能参数中指向基类指针的指针
- 功能参数转换为儿童类C
- 功能参数与函数调用不匹配