c++ - 折叠 std::initializer_list 的构造函数的参数列表与“正常”折叠
问题描述
我从 Jacek Galowicz 的 C++17 STL Cookbook 中学习了 C++17,并且有一个关于 lambdas 的示例:
template <typename... Ts> static auto multicall(Ts... functions)
{
return [=](auto x) { (void)std::initializer_list<int>{((void)functions(x), 0)...}; };
}
template <typename F, typename... Ts> static auto for_each(F f, Ts... xs)
{
(void)std::initializer_list<int>{((void)f(xs), 0)...};
}
static auto brace_print(char a, char b)
{
return [=](auto x) { std::cout << a << x << b << ", "; };
}
int main()
{
auto f(brace_print('(', ')'));
auto g(brace_print('[', ']'));
auto h(brace_print('{', '}'));
auto nl([](auto) { std::cout << '\n'; });
auto call_fgh(multicall(f, g, h, nl));
for_each(call_fgh, 1, 2, 3, 4, 5);
}
为什么在std::initializer_list
这里使用以及为什么void
使用这种强制转换(作者写道应该reinterpret_cast
使用而不是类似C的强制转换,但问题是为什么要使用这种强制转换)?
当我将multicall
andfor_each
函数更改为:
template <typename... Ts> static auto multicall(Ts... functions)
{
return [=](auto x) { (functions(x), ...); };
}
template <typename F, typename... Ts> static auto for_each(F f, Ts... xs)
{
(f(xs), ...);
}
一切都按预期工作,我得到相同的结果。
解决方案
出于某些原因,这本书的这一部分似乎以 C++14 方式运行。std::initializer_list
在 C++17 中引入折叠表达式以调用模拟可变参数调用之前,需要使用技巧。在 C++17 中,逗号操作符是绝对合法的
推荐阅读
- flutter - 底部溢出 100.0 pxl
- javascript - Nest 无法识别来自 fetch 请求的 POST
- raspberry-pi3 - 启动和重启后,如何在后台运行 rapberry pie 中的 python 代码?
- android - react-native-reanimated:androidJavadoc 错误在 android 设备上运行 react-native 应用程序
- dpdk - DPDK 在一个端口上获得太多 rx_crc_errors
- json - 使用 VBA-JSON 库将 excel 数据转换为 json
- java - 在 Android Java 中调用另一个类中的函数
- javascript - Python 和 JavaScript 链接
- reactjs - 具有 React 上下文的当前用户
- mysql - MySQL在加入后得到两条最近的记录