折叠表达式 (C++17 起)
来自 cppreference.cn
目录 |
[编辑] 语法
( pack op ... ) |
(1) | ||||||||
( ... op pack ) |
(2) | ||||||||
( pack op ... op init ) |
(3) | ||||||||
( init op ... op pack ) |
(4) | ||||||||
1) 一元右折叠。
2) 一元左折叠。
3) 二元右折叠。
4) 二元左折叠。
op | - | 以下 32 个二元运算符中的任意一个: + - * / % ^ & | = < > << >> += -= *= /= %= ^= &= |= <<= >>= == != <= >= && || , .* ->*。在二元折叠中,两个 op 必须相同。 |
pack | - | 一个包含未展开的包且其顶层不包含优先级低于转换的运算符的表达式(形式上为cast-expression) |
init | - | 一个不包含未展开的包且其顶层不包含优先级低于转换的运算符的表达式(形式上为cast-expression) |
请注意,开括号和闭括号是折叠表达式的必需部分。
[编辑] 解释
折叠表达式的实例化将表达式 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 或 pack 的表达式在顶层具有优先级低于转换的运算符,则必须将其用括号括起来
template<typename... Args> int sum(Args&&... args) { // return (args + ... + 1 * 2); // Error: operator with precedence below cast return (args + ... + (1 * 2)); // OK }
功能测试宏 | 值 | 标准 | 特性 |
---|---|---|---|
__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++ 标准。
缺陷报告 | 应用于 | 发布时的行为 | 正确的行为 |
---|---|---|---|
CWG 2611 | C++17 | 折叠表达式的展开结果未用括号括起来 | 用括号括起来 |