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.
Some time ago I wrote about a new way to implement runtime polymorphism which is based not on virtual functions but on std::visit and std::variant. Please have a look at this new blog post where I experiment with this approach on my home project. The experiment is more practical than artificial examples.
I took my old pet project from 2006, experimented, refactored it and made it more modern C++. Here are my lessons and six practical steps that you can apply in your projects.
Let’s start
Background And Test Project All changes that I describe here are based on my experience with a pet project which I dig out from the studies.
In my previous article on polymorphic allocators, we discussed some basic ideas. For example, you’ve seen a pmr::vector that holds pmr::string using a monotonic resource. How about using a custom type in such a container? How to enable it? Let’s see.
The Goal In the previous article there was similar code:
Up to (and including) C++17 if you wanted to check the start or the end in a string you have to use custom solutions, boost or other third-party libraries. Fortunately, this changes with C++20.
See the article where I’ll show you the new functionalities and discuss a couple of examples.
We’re on the last day of the lambda week. We have all the essential knowledge, and now we can learn some tricks!
The Series This blog post is a part of the series on lambdas:
The syntax changes (Tuesday 4th August) Capturing things (Wednesday 5th August) Going generic (Thursday 6th August) Tricks (Friday 5th August)(this post) +[]() Have a closer look:
We’re in the third day of the lambda week. So far, you’ve learned basic syntax and how to capture things. Another important aspect is that lambdas can also be used in the “generic” scenarios. This is especially possible since C++14 where we got generic lambdas (auto arguments), and then in C++20, you can even specify a template lambda!
We’re in the second day of the lambda week. Today you’ll learn about the options you have when you want to capture things from the external scope. Local variables, global, static, variadic packs, this pointer… what’s possible and what’s not?
The Series This blog post is a part of the series on lambdas:
Let’s start the week with Lambda Expressions. The plan is to have a set of concise articles presenting core elements of lambda expressions. Today you can see how the syntax has evolved starting since C++11 and what are the latest changes in C++20.
The Series This blog post is a part of the series on lambdas:
I’m pleased to announce that I’ve finished the work on the latest update for C++ Lambda Story! This makes the book complete now, and I can finally set its status to 100%! Read on to see the latest changes. It got more than 50 new pages!
Changes Last time the book had 95 pages, and with the recent additions, it reached 146!
Last Tuesday, 21th July, I had a pleasure to talk about [[no_unique_address]] on our Cracow C++ User Group online meeting.
Here are the slides and additional comments from the presentation.
Some Issues Our C++ Cracow User group:
https://www.meetup.com/C-User-Group-Cracow/
We also experienced the problems related to COVID situation, and for two months - in March and April, we had to cancel our regular monthly meetings.
The concept of a polymorphic allocator from C++17 is an enhancement to standard allocators from the Standard Library.
It’s much easier to use than a regular allocator and allows containers to have the same type while having a different allocator, or even a possibility to change allocators at runtime.
Let’s see how we can use it and hack to see the growth of std::vector containers.
Let’s consider a simple task: “Use a worker thread to compute a value”.
In the source it can look like the following line:
std::thread t([]() { auto res = perform_long_computation(); }; We have a thread, and it’s ready to start. But how to get the computed value efficiently out of that thread?