Tag: std-ranges
-
Why does GCC copy object for each comparison in `std::ranges::max`?
10 Consider the following example (Godbolt): #include <vector> #include <iostream> #include <ranges> #include <algorithm> struct A { A() {} A( const A& ) { std::cout << "Copyn"; } A( A&& ) noexcept { std::cout << "Moven"; } A& operator=(const A&) { std::cout << "Copy assignedn"; return *this; } A& operator=( A&& ) noexcept { std::cout…
-
std::ranges::find vs std::find
8 Consider this code: #include <vector> #include <iostream> #include <cstdint> #include <ranges> int main() { struct S { int a; int b; bool operator==(int other) const { return a == other; } }; std::vector<S> iv{ {1, 2}, {3, 4} }; // this works if (auto const it{std::find(iv.begin(), iv.end(), 1)}; it != iv.end()) { std::cout <<…
-
Why does ranges::for_each return the function?
10 The legacy std::for_each returns function as the standard only requires Function to meet Cpp17MoveConstructible according to [alg.foreach]: template<class InputIterator, class Function> constexpr Function for_each(InputIterator first, InputIterator last, Function f); Preconditions: Function meets the Cpp17MoveConstructible requirements. [Note: Function need not meet the requirements of Cpp17CopyConstructible. end note] This is reasonable since the user may want to…
-
C++20’s std::views::filter not filtering the view correctly
7 I was writing a simple C++ program that generates a list of random integer values from a normal distribution, then takes first N generated items and filters them so that their absolute value is greater than 42. The code i wrote is the following: int main() { //convenience namespace rng = std::ranges; namespace view…