The above field is supplemented with consent to receive a newsletter containing information and marketing content about the cppstories.com portal from Bartłomiej Filipek codebf based in Krakow. The consent may be withdrawn at any time. See the full Privacy Policy.
In this article, I’ll show another technique that avoids mixing signed and unsigned types.
In my article Integer Conversions and Safe Comparisons in C++20 we learned about cmp_* integer comparison functions that allow us to compare various types of integer types. The functions are safe because they help with mixing signed and unsigned types.
Sometimes, If you mix different integer types in an expression, you might end up with tricky cases. For example, comparing long with size_t might give different results than long with unsigned short. C++20 brings some help, and there’s no need to learn all the complex rules :)
Conversion and Ranks Let’s have a look at two comparisons:
C++ is famous… or infamous for its complex initialization syntax. In this article, I’ll show you around 20 ways to initialize simple std::string variables. Can we somehow make it easier to understand?
Default values Have a look:
void foo() { std::string str0; std::string str1 {}; } We have two local variables (with automatic storage duration), str0 is default initialized, while str1 is value initialized.
While there are many code analysis tools for C++, why not write it from scratch? This article will introduce you to an open-source C++ static analysis tool that you might find useful or at least interesting.
This is a guest post from Greg Utas.
Greg was chief software architect of the call servers used in AT&T’s wireless network.
Before C++17, we had a few quite ugly-looking ways to write static if (if that works at compile time). For example, you could use tag dispatching or SFINAE. Fortunately, that’s changed, and we can now benefit from if constexpr and concepts from C++20!
Let’s see how we can use it and replace some std::enable_if code.
In this blog post I’ll show you a couple of interesting examples with lambda expressions. Do you know how to write a recursive lambda? Store them in a container? Or invoke at compile time?
See in the article.
Updated in August 2022: Added C++23 improvements.
1. Recursive Lambda with std::function Writing a recursive function is relatively straightforward: inside a function definition, you can call the same function by its name.
std::format is a large and powerful addition in C++20 that allows us to format text into strings efficiently. It adds Python-style formatting with safety and ease of use.
This article will show you how to implement custom formatters that fit into this new std::format architecture.
Quick Introduction to std::format Here’s the Hello World example:
Working with data members and class design is essential to almost any project in C++. In this article, I gathered five topics that I hope will get you curious about the internals of C++.
1. Changing status of aggregates Intuitively a simple class type, or an array should be treated as “aggregate” type.
With Modern C++ and each revision of the Standard, we get more comfortable ways to initialize data members. There’s non-static data member initialization (from C++11) and inline variables (for static members since C++17).
In this blog post, you’ll learn how to use the syntax and how it has changed over the years.
This article is the third and the last one in the mini-series about ranges algorithms. We’ll look at some sorting, searching, and remaining algorithms. We’ll also have a glimpse of cool C++23 improvements in this area.
Let’s go.
Before we start Key observations for std::ranges algorithms:
Ranges algorithms are defined in the <algorithm> header, while the ranges infrastructure and core types are defined in the <ranges> header.
C++11 has been around for around 11 years and C++14 for 8. From my experience, I see that even today, many companies struggle to use those standards in production in the most efficient way. As always, with new stuff came benefits, risks, and increased learning effort. Fortunately, with a new book written by top C++ Experts, we have a solid guide on what is safe and what might be problematic in Modern C++.
In the previous article in the Ranges series, I covered some basics and non-modifying operations. Today it’s time for algorithms like transform, copy, generate, shuffle, and many more…. and there’s rotate as well :)
Let’s go.
Before we start Key observations for std::ranges algorithms:
Ranges algorithms are defined in the <algorithm> header, while the ranges infrastructure and core types are defined in the <ranges> header.