为什么左折叠表达式不反转右折叠表达式的输出?
Why doesn't a left fold expression invert the output of a right fold expression?
我正在看 C++17 fold expressions 我想知道为什么下面的程序会输出
4 5 6
4 5 6
两次 for_each
调用
template<typename F, typename... T>
void for_each1(F fun, T&&... args)
{
(fun (std::forward<T>(args)), ...);
}
template<typename F, typename... T>
void for_each2(F fun, T&&... args)
{
(..., fun (std::forward<T>(args)));
}
int main()
{
for_each1([](auto i) { std::cout << i << std::endl; }, 4, 5, 6);
std::cout << "-" << std::endl;
for_each2([](auto i) { std::cout << i << std::endl; }, 4, 5, 6);
}
我以为二折表达式是为了倒序输出数字
6 5 4
怎么结果一样?
根据 § 14.5.3/9
The instantiation of a fold-expression produces:
(9.1) — ((E1 op E2) op · · · ) op EN for a unary left fold,
(9.2) — E1 op (· · · op (EN-1 op EN )) for a unary right fold,
(9.3) — (((E op E1) op E2) op · · · ) op EN for a binary left fold, and
(9.4) — E1 op (· · · op (EN-1 op (EN op E))) for a binary right fold
In each case, op is the fold-operator, N is the number of elements in the pack expansion parameters, and each Ei is generated by instantiating the pattern and replacing each pack expansion parameter with its ith element.
在上面的代码中,它们都是一元折叠表达式,它们的扩展是
template<typename F, typename... T>
void for_each1(F fun, T&&... args) {
// Unary right fold (fun(args_0) , (fun(args_1) , (fun(args_2) , ...)))
(fun (std::forward<T>(args)), ...);
}
template<typename F, typename... T>
void for_each2(F fun, T&&... args) {
// Unary left fold ((fun(args_0) , fun(args_1)) , fun(args_2)) , ...
(..., fun (std::forward<T>(args)));
}
因此表达式具有与 comma operator 定义的相同 求值顺序 ,因此输出相同。
致谢:感谢我的朋友 Marco 首先提出了最初的问题,让我有机会解决这个 可能具有误导性的问题 问题。
我正在看 C++17 fold expressions 我想知道为什么下面的程序会输出
4 5 6
4 5 6
两次 for_each
调用
template<typename F, typename... T>
void for_each1(F fun, T&&... args)
{
(fun (std::forward<T>(args)), ...);
}
template<typename F, typename... T>
void for_each2(F fun, T&&... args)
{
(..., fun (std::forward<T>(args)));
}
int main()
{
for_each1([](auto i) { std::cout << i << std::endl; }, 4, 5, 6);
std::cout << "-" << std::endl;
for_each2([](auto i) { std::cout << i << std::endl; }, 4, 5, 6);
}
我以为二折表达式是为了倒序输出数字
6 5 4
怎么结果一样?
根据 § 14.5.3/9
The instantiation of a fold-expression produces:
(9.1) — ((E1 op E2) op · · · ) op EN for a unary left fold,
(9.2) — E1 op (· · · op (EN-1 op EN )) for a unary right fold,
(9.3) — (((E op E1) op E2) op · · · ) op EN for a binary left fold, and
(9.4) — E1 op (· · · op (EN-1 op (EN op E))) for a binary right fold
In each case, op is the fold-operator, N is the number of elements in the pack expansion parameters, and each Ei is generated by instantiating the pattern and replacing each pack expansion parameter with its ith element.
在上面的代码中,它们都是一元折叠表达式,它们的扩展是
template<typename F, typename... T>
void for_each1(F fun, T&&... args) {
// Unary right fold (fun(args_0) , (fun(args_1) , (fun(args_2) , ...)))
(fun (std::forward<T>(args)), ...);
}
template<typename F, typename... T>
void for_each2(F fun, T&&... args) {
// Unary left fold ((fun(args_0) , fun(args_1)) , fun(args_2)) , ...
(..., fun (std::forward<T>(args)));
}
因此表达式具有与 comma operator 定义的相同 求值顺序 ,因此输出相同。
致谢:感谢我的朋友 Marco 首先提出了最初的问题,让我有机会解决这个 可能具有误导性的问题 问题。