- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我希望创建一个函数,它接受任意数量的仿函数对象或更一般地只是可调用对象(不同类型)并将它们应用于内部数据结构。该函数将在我的代码中的不同点与不同数量的仿函数一起使用。
我考虑过使用可变参数模板,而不是制作不同的版本来接受 1、2、3... 等和重复代码。
我找到了一个解决方案,我将在下面发布作为答案,因为我在 Google 上找不到任何关于此的具体内容,其他人可能会觉得它很有用。但是,如果有人有更好的想法,请发布它们。我觉得应该有一个标准的方法来做到这一点?
我知道这行不通,但我的第一次尝试是
#include <iostream>
using namespace std;
struct FunctorA {
void operator() () {
cout << "FunctorA" << endl;
}
};
struct FunctorB {
void operator() () {
cout << "FunctorB" << endl;
}
};
template<typename... Fs>
void apply_functors(Fs... fs) {
fs()...; // will not work - can only expand packs in certain situations (e.g. as funciton
}
int main(void) {
apply_functors(FunctorA(),FunctorB());
apply_functors(FunctorA());
apply_functors([]()->void{ cout << "Lambda" << endl; });
return 0;
}
但是这不起作用,因为我们只允许在 certain situations 中扩展参数包像这样的免费不是一个。
我的下一个想法是创建一个虚拟函数,它什么都不做,但我可以将仿函数作为参数传递给它,然后让它们展开。这背后的原因是函数参数是我们可以扩展参数包的情况之一。
template<typename... Fs>
void _dummyF_impl(Fs... fs){}
template<typename... Fs>
void apply_functors(Fs... fs) {
_dummyF_impl( fs()... ); // only works if all functors return a value (else we get "invalid use of 'void'")
}
但是,这不起作用,因为仿函数从它们的 operator()
中返回 void,我们不能将 void 作为参数传递给函数。
最佳答案
在花括号初始化器内扩展参数包具有保证从左到右评估的额外好处(函数参数列表不是这种情况)。您还应该考虑使用完美转发,here's why .
#include <initializer_list> //
#include <utility> // std::forward
template<typename... Fs>
void apply_functors(Fs&&... fs)
{
auto list = { (std::forward<Fs>(fs)(), 0)... };
// ^^^^ deduced as std::initializer_list
}
原则上相同,但这次没有必要包含 <initializer_list>
是一个可变参数模板构造函数,您也可以使用大括号初始化器调用它:
struct dummy {
template<typename... Fs>
dummy(Fs&&... fs) { }
};
template<typename... Fs>
void apply_functors(Fs&&... fs)
{
dummy { (std::forward<Fs>(fs)(), 0)... }; // evaluated left-to-right, too
}
关于c++ - 接受仿函数/可调用对象的可变参数函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18077259/
我正在本地编程应用程序,但是当我迁移到服务器时,使用此行出现解析错误: if(!is_array($data[array_keys($data)[0]])) 返回值: Parse error: syn
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 9 年前。 Improv
我已经开始尝试用 C++ 学习 Winsock,但我遇到了一些问题。我首先遵循 MSDN 上的 Winsock 示例(一个基本的客户端-服务器)。 http://msdn.microsoft.com/
我有一段使用 epoll 的代码,但它有问题。当我运行它时,它给出输出:服务器套接字()没问题......服务器绑定(bind)()没问题......3个4个接受:无效参数 我在 ubuntu lin
我正在寻找一种方法来接受 $_GET 变量作为文件路径,例如 - /page/test将转换为 page.php?page=test .我已经搜索过,但找不到任何可以帮助我的东西,因此我在这里问。 我
我想要一个只接受从 0 到 9 和减号的浮点数的正则表达式。 请帮忙。 最佳答案 ^[-+]?[0-9]*\.?[0-9]+$ ^ - 字符串开头 [-+]? - 0 或 1 符号指示符 [0-9]*
请问如何接受\r\n无需将其更改为 \\r\\n , 与 fgets . 我想让程序翻译 \r\n到换行符而不是将其打印为字符串。 当前代码: char buff[1024]; printf("Msg
我正在编写一个 Cocoa 应用程序,该应用程序需要在其 Dock 图标上接受已安装卷的滴落。它不是基于文档的;我打算将每个卷分派(dispatch)到 application:openFiles 中
我在 SQLite 中发现了这种意外行为。 SQLite 似乎接受 SQL 连接语法中的任意关键字。如果我不小心键入了 natural join 而不是 natural join,则会生成笛卡尔积。这
我在 Windows 窗体的同一个窗体上有一个 TreeView 和一个多行文本框。我有拖放设置,以便我可以将节点从 TreeView 拖到文本框并将文本插入文本框(这是有效的)。 我想增强这一点,以
我正在创建一棵类似于 D3 Layout Tree 的树并尝试绑定(bind)我的自定义 JSON 对象。切换树节点的代码如下。 function toggleAll(d) { if (d.c
所以,我希望能够向我的 DOB 字段发送空选项。 这是我的表单生成器: ->add('birthDate', DateType::class, array( 'widg
错误可以在这里看到:http://djaffry.selfip.com:8080/ 我希望索引页接受参数,无论是 mysite.com/search/param_here 或 mysite.com/?
我想知道标准 Scala 解析器组合器是否包含一个解析器,该解析器接受 Scala 语言本身也接受的相同标识符(如 Scala 语言规范第 1.1 节中所指定)。 StdTokenParsers 特征
我暂时使用以下行在我的 Android 手机上创建 ServerSocket: socketl = new ServerSocket(port, 0, InetAddress.getByName("1
今天早上刚刚发布了一个应用程序,我在所有可能的设备和模拟器上测试了它,但是当我从商店下载它时,应用程序在启动时崩溃。我在很多设备和iOS版本上测试过,但结果都是一样的: Incident Identi
我想要 5 个名字中最长的一个。我想我应该使用 compareTo() 方法或 length()? 输出必须是这样的: enter 5 names : Joey Mark Catherine Zach
抱歉,我不熟悉泛型,我可以创建一个如下所示的泛型类: public class InfoField { } 上面的类可以接受如下值: , User> 但是我可以有接受 map 对象作为值的类吗?
我想让一个单元格等于它上面的单元格。 当我写作时 =address(row()-1;column()) 它只是写入上面单元格的名称(例如,对于 B2,它写入 $B$1) 是否有一个函数可以输入地址并放
我正在使用Asm访问java字节码来获取方法信息。看来ClassReader类的accept方法是异步调用的,所以不可能立即获取方法信息。我该怎么办? 最佳答案 实际上,accept 方法是从您自己的
我是一名优秀的程序员,十分优秀!