cxxomfort  rel.20211024
Simple backports for C++ - https://ryan.gulix.cl/fossil.cgi/cxxomfort/
<algorithm>

Backports related to Standard header <algorithm>

// or as part of:

Interfaces

Backports defined in this section:

Ommitted:

Non-backport features ( "Implementation Fixes" and others ):

Constexpr-capable algorithms

Starting with C++20, a high number of features in <algorithm> are made constexpr capable.

This header offers a number of constexpr -capable implementations for some algorithms that can be made constexpr with only C++11 or C++14 requirement but weren't in their corresponding Standard. Because the names can not be reused by cxxomfort without UB as they might end up sharing the same number of arguments (see eg.: equal() / mismatch() in C++14) , they are instead provided via a new mechanism:

The functions are provided in namespace cxxomfort::fix and lead with a constexpr_tag argument. For example, the following invocations will call the constexpr-capable functions in cxxomfort:

// may or may not work in constexpr,
// depending on *stdlib* (not on compiler)
constexpr int* px = std::find( arr, arr+N, 8 );
// should work always in constexpr
// if the *compiler* is capable
using cxxomfort::constexpr_tag
constexpr int* px = cxxomfort::fix::find( constexpr_tag, arr, arr+N, 8 );

These algorithms are:

Note
Without relaxed constexpr, how much work can these algorithms do depends on the compiler's recursion level for constexpr . Consult your compiler manual for more information.
These algorithms can only work constexpr with native arrays, and with constexpr types whose iterators are pointer types (eg.: basic_string with default allocator).

Demonstration:

Copies and prints elements from a list via copy_if() and for_each_n() .

struct {
template <typename Numeric>
bool operator() (Numeric n) const {
return n % 2 == 0;
}
} even= {};
struct {
template <typename T>
SS& operator() (T const& t) const {
return os<< t;
}
std::ostream& os;
} print (std::cout);
list<int> s1;
generate( begin(s1), end(s1), rand);
vector<int> s2;
copy_if( begin(s1), end(s1), back_inserter(s2), even);
for_each_n( begin(s1), end(s1), print);

Examples

For other examples see the following entries in cxxomfort-examples :

See Also

See also
cxxomfort/algorithm.hpp (header reference)
header/algorithm @ cppreference (cppreference)
<numeric> (std numeric algorithms) , Cxxomfort <algorithm> Supplements (supplemental)
Member cxxomfort::cxxostd::count (It ini, It fin, T const &tv)
Member cxxomfort::cxxostd::count_if (It ini, It fin, Pred p)
Member cxxomfort::cxxostd::fill (It ini, It fin, T const &tv)
Fills a sequence with a given value
Member cxxomfort::cxxostd::fill_n (It ini, Numeric n, T const &tv)
Fills a sequence with a given value
Member cxxomfort::cxxostd::for_each_n (II first, Size n, UnaryFunction f)
Invoke a function over the elements of a sequence.
Member cxxomfort::cxxostd::is_sorted (Iterator ini, Iterator fin, Compare less)
Member cxxomfort::cxxostd::minmax (T const &a, T const &b, Comparator Less)
Find the minimum and maximum elements in a sequence
Member cxxomfort::cxxostd::minmax_element (FIterator ini, FIterator fin, Comparator less)
Member cxxomfort::cxxostd::next_permutation (BidirIt ini, BidirIt fin, Compare Less)
Member cxxomfort::cxxostd::partition_copy (InpI ini, InpI const fin, OutI1 Dtrue, OutI2 Dfalse, Predicate f)
Conditionally copy data to either of two destinations. Copy elements to two locations conditionally.
Member cxxomfort::cxxostd::prev_permutation (BidirIt ini, BidirIt fin, Compare less)
Member cxxomfort::cxxostd::shuffle (RAIterator ini, RAIterator fin, URNG &g)
Member cxxomfort::move (IIt ini, IIt fin, OIt dest)
Member cxxomfort::move_backward (IIt ini, IIt fin, OIt dest)
Class ratio< N, D >
Rational arithmetic type