Performance of std::string_view vs std::string from C++17

How much is std::string_view faster than standard std::string operations? Have a look at a few examples where I compare std::string_view against std::string. Intro I was looking for some examples of string_view, and after a while, I got curious about the performance gain we might get. string_view is conceptually only a view of the string: usually implemented as[ptr, length].

READ MORE...

In-Place Construction for std::any, std::variant and std::optional

When you read articles or reference pages for std::any, std::optional or std::variant you might notice a few helper types called in_place_* available in constructors. Why do we need such syntax? Is this more efficient than “standard” construction? Intro We have the following in_place helper types: std::in_place_t type and a global value std::in_place - used for std::optional std::in_place_type_t type and a global value std::in_place_type - used for std::variant and std::any std::in_place_index_t type and a global value std::in_place_index - used for std::variant The helpers are used to efficiently initialise objects “in-place” - without additional temporary copy or move operations.

READ MORE...

Parallel STL And Filesystem: Files Word Count Example

Last week you might have read about a few examples of parallel algorithms. Today I have one more application that combines the ideas from the previous post. We’ll use parallel algorithms and the standard filesystem to count words in all text files in a given directory. The Case In my previous post, there were two examples: one with iterating over a directory and counting the files sizes and the next one about counting words in a string.

READ MORE...

Everything You Need to Know About std::any from C++17

With std::optional you can represent some Type or nothing. With std::variant you can wrap several variants into one entity. And C++17 gives us one more wrapper type: std::any that can hold anything in a type-safe way. The Basics So far in the Standard C++, you had not many options when it comes to holding variable types in a variable.

READ MORE...

A Wall of Your std::optional Examples

Two weeks ago I asked you for help: I wanted to build a wall of examples of std::optional. I’m very grateful that a lot of you responded and I could move forward with the plan! You’re amazing! Let’s dive in the examples my readers have sent me! A Reminder To remind, I asked for some real-life examples of std::optional.

READ MORE...

Everything You Need to Know About std::variant from C++17

Around the time C++17 was being standardized I saw magical terms like “discriminated union”, “type-safe union” or “sum type” floating around. Later it appeared to mean the same type: “variant”. Let’s see how this brand new std::variant from C++17 works and where it might be useful. The Basics In my experience, I haven’t used unions much.

READ MORE...

Show me your code: std::optional

Show me your code! I’d like to run a little experiment. Let’s build a wall of examples of std::optional! Intro In the last three articles of my C++17 STL series I’ve been discussing how to use std::optional. I can talk and talk… or write and write… but I’m wondering how do you use this wrapper type?

READ MORE...

Error Handling and std::optional

In my last two posts in the C++17 STL series, I covered how to use std::optional. This wrapper type (also called “vocabulary type”) is handy when you’d like to express that something is ‘nullable’ and might be ‘empty’. For example, you can return std::nullopt to indicate that the code generated an error… but it this the best choice?

READ MORE...