A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://en.cppreference.com/w/cpp/language/fold below:

Fold expressions (since C++17) - cppreference.com

Reduces (folds) a pack over a binary operator.

[edit] Syntax ( pack op ... ) (1) ( ... op pack ) (2) ( pack op ... op init ) (3) ( init op ... op pack ) (4)

1) Unary right fold.

2) Unary left fold.

3) Binary right fold.

4) Binary left fold.

op - any of the following 32 binary operators: + - * / % ^ & | = < > << >> += -= *= /= %= ^= &= |= <<= >>= == != <= >= && || , .* ->*. In a binary fold, both ops must be the same. pack - an expression that contains an unexpanded pack and does not contain an operator with precedence lower than cast at the top level (formally, a cast-expression) init - an expression that does not contain an unexpanded pack and does not contain an operator with precedence lower than cast at the top level (formally, a cast-expression)

Note that the opening and closing parentheses are a required part of the fold expression.

[edit] Explanation

The instantiation of a fold expression expands the expression e as follows:

1) Unary right fold (E op ...) becomes (E1 op (... op (EN-1 op EN)))

2) Unary left fold (... op E) becomes (((E1 op E2) op ...) op EN)

3) Binary right fold (E op ... op I) becomes (E1 op (... op (EN−1 op (EN op I))))

4) Binary left fold (I op ... op E) becomes ((((I op E1) op E2) op ...) op EN)

(where N is the number of elements in the pack expansion)

For example,

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

When a unary fold is used with a pack expansion of length zero, only the following operators are allowed:

1) Logical AND (&&). The value for the empty pack is true.

2) Logical OR (||). The value for the empty pack is false.

3) The comma operator (,). The value for the empty pack is void().

[edit] Notes

If the expression used as init or as pack has an operator with precedence below cast at the top level, it must be parenthesized:

template<typename... Args>
int sum(Args&&... args)
{
//  return (args + ... + 1 * 2);   // Error: operator with precedence below cast
    return (args + ... + (1 * 2)); // OK
}
[edit] Example
#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);
}

Output:

123abc
255 65535 4294967295 
6 2 45 12 1 2 9
[edit] References
[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior CWG 2611 C++17 the expansion results of fold expressions were not enclosed with parentheses enclosed with parentheses

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4