Tag: c++20
-
Why can adding a month to a valid date result in an invalid date instead of adjusting the result to be valid?
16 The <chrono> library allows dates to silently fall into a state of !ok(). For example: #include <chrono> #include <iostream> int main() { using namespace std; using namespace chrono; auto date = 2023y/October/31; cout << date.ok() << ‘n’; date += months{1}; cout << date.ok() << ‘n’; } Output: 1 0 Demo. I get that Oct…
-
When is it ok to be !ok() with C++20 chrono dates?
10 The <chrono> library allows dates to silently fall into a state of !ok(). For example: #include <chrono> #include <iostream> int main() { using namespace std; using namespace chrono; auto date = 2023y/October/31; cout << date.ok() << ‘n’; date += months{1}; cout << date.ok() << ‘n’; } Output: 1 0 Demo. I get that Oct…
-
fmt format %H:%M:%S without decimals
7 I am trying to format a std::chrono::duration object to the format HH:MM::SS, e.g. 16:42:02 being the hours (16), the minutes (42), and the seconds (2). the library fmt offers useful formatting specifiers for this: using namespace std::chrono; auto start = high_resolution_clock::now(); auto end = start + 4s; fmt::print("{:%H:%M:%S} n", end); which unfortuantely prints the…
-
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…
-
Why does C++ have no std::invocable_r concept?
11 C++20 added concepts, and the standard library includes quite a few of them. One concept in particular caught my eye: std::invocable which validates that a functor can be invoked with a set of arguments. std::invocable is just syntactic sugar for std::is_invocable. However, the standard library further defines std::is_invocable_r which tests whether a functor can…
-
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…
-
C++20 use concept to restrict an ‘auto’ non-type template argument?
9 I have a template class taking several non-type arguments (GPIO descriptors). Auto is used to allow several different GPIO implementations to be used. These implementations can’t derive from a common base class. Can I use a concept to restrict these arguments to ones with the right interface? I got a concept but it’s not…