c++ - 如何将 lambda/std::function/Functor 传递给可变参数函数
问题描述
我有一个像这样的可变参数函数:
template <class... PickComponents, size_t... PickComponentIndices>
void iterateEntitiesInternal(
const std::function<void(Entity, const PickComponents &...)> &iterFn,
std::index_sequence<PickComponentIndices...> sequence
);
template <class... PickComponents>
void iterateEntities(const std::function<void(Entity, const PickComponents &...)> &iterFn) {
iterateEntitiesInternal(iterFn, std::index_sequence_for<PickComponents...>{});
}
我希望能够像这样调用函数:
iterateEntities<A, B, C>([](Entity entity, const auto &c1, const auto &c2, const auto &c3) {
// ...
});
有没有办法让这个工作?我尝试创建一个模板结构,如:
template <typename T> struct IterFnType { typedef T type; };
并像这样使用它:
template <class... PickComponents>
void iterateEntities(const typename IterFnType<std::function<void(Entity, const PickComponents &...)>>::type &iterFn);
但它在 GCC 中给了我一个编译器错误:
错误:'typename liquid::EntityStorageSparseSet::IterFnType' 名称 'template<class ... ComponentTypes> template structliquid::EntityStorageSparseSet::IterFnType',它不是一个类型
将 lambdas 传递给可变参数函数的正确方法是什么?
解决方案
推荐阅读
- json - 我需要从两个变量之间的单行 json-data 中提取数据(Powershell)
- node.js - 使用屏幕截图诊断在 Heroku 上运行的 Puppeteer 上的错误
- javascript - Outlook 插件:如何使用 Outlook 桌面显示 console.log?
- c - 如何在 C 中创建分区
- angular - 使用 Chart.js 在条形图中显示空值或空值的省略号
- c# - Discord bot 有时在通过命令参数传递提及时找不到用户
- c++ - 避免在嵌入式上使用 func(char *) api
- javascript - 根据 ApexChart 中的缩放动态更改标签
- tensorflow - 在 Darknet 上使用 YOLO 预训练权重进行训练
- powershell - 用于 IIS 站点计数的 Powershell 脚本