C++20 Ranges, Projections, std::invoke and if constexpr

Continuing the topic from last week, let’s dive into the topic of std::invoke. This helper template function helps with uniform syntax call for various callable object types and can greately reduce the complexity of our generic code. Ranges and Projections In C++20 there are handful of rangified algorithms. As a simple example let’s say we want to sort a vector of integers:

READ MORE...

How to Convert Numbers into Text with std::to_char in C++17

In this post, I’ll show you how to use the newest, low-level, conversion routines form C++17. With the new functionality, you can quickly transform numbers into text and have super performance compared to previous techniques. Intro Until C++17, we had several ways of converting numbers into strings: sprintf / snprintf stringstream to_string itoa and 3rd-party libraries like boost - lexical cast And with C++17 we get another option: std::to_chars (along with the corresponding method from_chars) !

READ MORE...

How To Detect Function Overloads in C++17, std::from_chars Example

The problem: a library function offers several overloads, but depending on the implementation/compiler, some of the overloads are not available. How to check the existence of an overload? And how to provide a safe fallback? In this article, I’ll show you a background “theory” and one case - std::from_chars that exposes full support for numbers or only integer support (in GCC, Clang).

READ MORE...

[Quick Case] Surprising Conversions of const char* to bool

If you have two function overloads foo(): one is taking const std::string& and the other taking bool. Which one of them will be selected when you call foo("hello world"); ? Let’s see where such a case might bite us and cause troubles? Intro Here’s the example once again void foo(const std::string& in) { std::cout << in << '\n'; } void foo(bool in) { std::cout << "bool: " << in << '\n';} foo("Hello World"); What’s the output?

READ MORE...

std:filesystem::file_size Advantages and Differences

Subtitle: Learning std::filesystem through file_size routines. Last week I wrote a short post that explained how to use std::filesystem::file_size. Today I’d like to continue and show some significant differences that this new functionality has over the “older” techniques (like reading a file and getting its file position). We’ll also learn something about permissions and how to manage them in std::filesystem.

READ MORE...

Let's Talk About String Operations in C++17

In September our local C++ User Group started a “new year” of meetings after a little break in August. I had a pleasure to give a talk about string operations in C++17. Here are the slides and additional comments. The Talk For my book I wrote a lot of content about string_view, std::searcher and std::to_chars, std::from_chars and I wanted to make a short summary of those features.

READ MORE...

Preprocessing Phase for C++17's Searchers

Searchers from C++17 are a new way to perform efficient pattern lookups. The new standard offers three searchers: default_searcher , boyer_moore_searcher and boyer_moore_horspool_searcher. The last two implements algorithms that require some additional preprocessing for the input pattern. Is there a chance to separate preprocessing time from the search time?

READ MORE...