My Project
Functions | Variables
for_each.hpp File Reference
#include "details/for_each_impl.hpp"

Go to the source code of this file.

Functions

template<typename... ARGS>
constexpr void bsl::for_each (ARGS &&... args) noexcept(noexcept(//-- details::for_each_impl< ARGS... >::call(bsl::forward< ARGS >(args)...)))
 Loops over a view or a pair of iterators, calling a provided function on each iteration. The provided function can take on the following signatures: More...
 

Variables

constexpr bool bsl::for_each_break {false}
 Tells a foreach to stop its execution (same as "break")
 
constexpr bool bsl::for_each_continue {true}
 Tells a foreach to continue its execution (same as "continue")
 

Detailed Description

Function Documentation

◆ for_each()

template<typename... ARGS>
constexpr void bsl::for_each ( ARGS &&...  args)
noexcept

Loops over a view or a pair of iterators, calling a provided function on each iteration. The provided function can take on the following signatures:

  • void(T &elem)
  • void(T &elem, bsl::uintmax index)
  • bool(T &elem)
  • bool(T &elem, bsl::uintmax index) The boolean versions of this function allow you to return either bsl::for_each_break (to break from the loop) or for_each_continue (to continue the loop). Note that if you are using the void versions of the loop, you can continue by simply returning. The bool versions are only needed if you need to break from the loop, in which case bsl::for_each_continue should be used to return from the function. In addition to the different function signatures that you can provide, you can also provide bsl::for_each with either a subclass of a bsl::view, or two iterators (a begin and end iterator), which will be used to perform the loop. The bsl::for_each function acts like a ranged for loop when given a subclass of a bsl::view, and acts like a traditional for loop when given two iterators. Note that you can use a reverse iterator to loop in reverse, and you can use the BSL specific iter() functions that allow you to create your own begin and end iterators so that you can control the position and number of elements the loop will perform. For more information, see the following example:
    #include <bsl/for_each.hpp>
    #include <bsl/debug.hpp>
    namespace bsl
    {
    inline void
    example_for_each_overview() noexcept
    {
    constexpr bsl::string_view msg{"Hello"};
    constexpr bsl::uintmax i1{1U};
    constexpr bsl::uintmax i4{4U};
    constexpr char_type co{'o'};
    // ---------------------------------------------------------------------
    bsl::print() << "example 1:\n";
    bsl::for_each(msg, [](auto &e) noexcept {
    bsl::print() << e;
    });
    bsl::print() << "\n\n";
    // ---------------------------------------------------------------------
    bsl::print() << "example 2:\n";
    bsl::for_each(msg, [](auto &e, auto const i) noexcept {
    bsl::print() << "element [" << i << "] == " << e << bsl::endl;
    });
    // ---------------------------------------------------------------------
    bsl::print() << "example 3:\n";
    bsl::for_each(msg, [](auto &e) noexcept -> bool {
    if (co == e) {
    }
    bsl::print() << e;
    });
    bsl::print() << "\n\n";
    // ---------------------------------------------------------------------
    bsl::print() << "example 4:\n";
    bsl::for_each(msg.begin(), msg.end(), [](auto &e) noexcept {
    bsl::print() << e;
    });
    bsl::print() << "\n\n";
    // ---------------------------------------------------------------------
    bsl::print() << "example 5:\n";
    bsl::for_each(msg.iter(i1), msg.iter(i4), [](auto &e) noexcept {
    bsl::print() << e;
    });
    bsl::print() << "\n\n";
    // ---------------------------------------------------------------------
    bsl::print() << "example 6:\n";
    bsl::for_each(msg.rbegin(), msg.rend(), [](auto &e) noexcept {
    bsl::print() << e;
    });
    bsl::print() << "\n\n";
    // ---------------------------------------------------------------------
    bsl::print() << "example 7:\n";
    bsl::for_each(msg.rbegin(), msg.rend(), [](auto &e, auto const i) noexcept {
    bsl::print() << "element [" << i << "] == " << e << bsl::endl;
    });
    }
    }
Template Parameters
ARGSthe types of arguments passed to bsl::for_each.
Parameters
argscan either be a subclass of bsl::view and a lambda, or a pair of iterators and a lambda.