折叠表达式 (自 C++17)
来自 cppreference.cn
归约(折叠)二元运算符上的包。
目录 |
[[编辑]] 语法
( 包 op ... ) |
(1) | ||||||||
( ... op 包 ) |
(2) | ||||||||
( 包 op ... op init ) |
(3) | ||||||||
( init op ... op 包 ) |
(4) | ||||||||
1) 一元右折叠。
2) 一元左折叠。
3) 二元右折叠。
4) 二元左折叠。
op | - | 下列 32 个二元运算符中的任何一个: + - * / % ^ & | = < > << >> += -= *= /= %= ^= &= |= <<= >>= == != <= >= && || , .* ->*。在二元折叠中,两个 op 必须相同。 |
包 | - | 含有未展开包且不含顶层优先级低于强制转型运算符的运算符的表达式(正式地,为强制转型表达式) |
init | - | 不含未展开包且不含顶层优先级低于强制转型运算符的运算符的表达式(正式地,为强制转型表达式) |
注意,开括号和闭括号是折叠表达式的必要部分。
[[编辑]] 解释
折叠表达式的实例化展开表达式 e 如下
1) 一元右折叠
(E
op ...)
变为 (E1
op (
... op (EN-1
op EN)))
2) 一元左折叠
(...
op E)
变为 (((E1
op E2)
op ...)
op EN)
3) 二元右折叠
(E
op ...
op I)
变为 (E1
op (
... op (EN−1
op (EN
op I))))
4) 二元左折叠
(I
op ...
op E)
变为 ((((I
op E1)
op E2)
op ...)
op EN)
(其中 N
是包展开中的元素数量)
例如,
template<typename... Args> bool all(Args... args) { return (... && args); } bool b = all(true, true, true, false); // within all(), the unary left fold expands as // return ((true && true) && true) && false; // b is false
当一元折叠与长度为零的包展开一起使用时,仅允许以下运算符
1) 逻辑与 (&&)。空包的值为 true。
2) 逻辑或 (||)。空包的值为 false。
3) 逗号运算符 (,)。空包的值为 void()。
[[编辑]] 注解
如果用作 init 或 包 的表达式具有顶层优先级低于强制转型的运算符,则必须将其括在括号中
template<typename... Args> int sum(Args&&... args) { // return (args + ... + 1 * 2); // Error: operator with precedence below cast return (args + ... + (1 * 2)); // OK }
特性测试宏 | 值 | Std | 特性 |
---|---|---|---|
__cpp_fold_expressions |
201603L |
(C++17) | 折叠表达式 |
[[编辑]] 示例
运行此代码
#include <climits> #include <concepts> #include <cstdint> #include <iostream> #include <limits> #include <type_traits> #include <utility> #include <vector> // Basic usage, folding variadic arguments over operator<< template<typename... Args> void printer(Args&&... args) { (std::cout << ... << args) << '\n'; } // Folding an expression that uses the pack directly over operator, template<typename... Ts> void print_limits() { ((std::cout << +std::numeric_limits<Ts>::max() << ' '), ...) << '\n'; } // Both a fold over operator&& using the pack // and over operator, using the variadic arguments template<typename T, typename... Args> void push_back_vec(std::vector<T>& v, Args&&... args) { static_assert((std::is_constructible_v<T, Args&&> && ...)); (v.push_back(std::forward<Args>(args)), ...); } // Using an integer sequence to execute an expression // N times by folding a lambda over operator, template<class T, std::size_t... dummy_pack> constexpr T bswap_impl(T i, std::index_sequence<dummy_pack...>) { T low_byte_mask = static_cast<unsigned char>(-1); T ret{}; ([&] { (void)dummy_pack; ret <<= CHAR_BIT; ret |= i & low_byte_mask; i >>= CHAR_BIT; }(), ...); return ret; } constexpr auto bswap(std::unsigned_integral auto i) { return bswap_impl(i, std::make_index_sequence<sizeof(i)>{}); } int main() { printer(1, 2, 3, "abc"); print_limits<uint8_t, uint16_t, uint32_t>(); std::vector<int> v; push_back_vec(v, 6, 2, 45, 12); push_back_vec(v, 1, 2, 9); for (int i : v) std::cout << i << ' '; std::cout << '\n'; static_assert(bswap<std::uint16_t>(0x1234u) == 0x3412u); static_assert(bswap<std::uint64_t>(0x0123456789abcdefull) == 0xefcdab8967452301ULL); }
输出
123abc 255 65535 4294967295 6 2 45 12 1 2 9
[[编辑]] 参考文献
- C++23 标准 (ISO/IEC 14882:2024)
- 7.5.6 折叠表达式 [expr.prim.fold]
- C++20 标准 (ISO/IEC 14882:2020)
- 7.5.6 折叠表达式 [expr.prim.fold]
- C++17 标准 (ISO/IEC 14882:2017)
- 8.1.6 折叠表达式 [expr.prim.fold]
[[编辑]] 缺陷报告
以下行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
CWG 2611 | C++17 | 折叠表达式的展开结果未用括号括起来 | 用括号括起来 |