首页 > 解决方案 > 折叠 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的强制转换,但问题是为什么要使用这种强制转换)?

当我将multicallandfor_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++lambdac++17fold

解决方案


出于某些原因,这本书的这一部分似乎以 C++14 方式运行。std::initializer_list在 C++17 中引入折叠表达式以调用模拟可变参数调用之前,需要使用技巧。在 C++17 中,逗号操作符是绝对合法的


推荐阅读