摺疊表示式 (C++17 起)
來自 cppreference.com
目錄 |
[編輯] 語法
( 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 | 摺疊表示式的展開結果未用括號括起來 | 用括號括起來 |