Do you know how many ways we can implement a filter function in C++?
While the problem is relatively easy to understand - take a container, copy elements that match a predicate and the return a new container - it’s good to exercise with the Standard Library and check a few ideas.
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:
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) !
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).
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?
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.
With C++17 we get another facility to handle the conversion between text and numbers. Why should we care about the new routines? Are they better in any way?
Intro C++, before C++17, offered several options when it comes to string conversion:
sprintf / snprintf sscanf atol strtol strstream stringstream to_string stoi and similar functions And with C++17 you get another option: std::from_chars!
C++17 brings us parallel algorithms. However, there are not many implementations where you can use the new features. The situation is getting better and better, as we have the MSVC implementation and now Intel’s version will soon be available as the base for libstdc++ for GCC. Since the library is important, I’ve decided to see how to use it and what it offers.
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.
std::visit is a powerful utility that allows you to call a function over a currently active type in std::variant. It does some magic to select the proper overload, and what’s more, it can support many variants at once.
Let’s have a look at a few examples of how to use this functionality.
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?
With C++17 you can now use more sophisticated algorithms for pattern searches! Now, you’ll have more control and a promising performance boost for many use cases.
Intro The naive approach of finding a pattern in a string is O(nm) (where n is the length of the whole string, m is the length of the pattern).