Last time in my blog post about How to Share Code with Const and Non-Const Functions in C++ I had a custom type declared and defined in one place (like in a header file). Recently, I tried to separate the declaration from implementation, and I got into a situation where one private function template was left.
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.
Two weeks ago, I announced a little game on my blog! Today I’d like to present some of the solutions you sent me and discuss a few things from various aspects of Modern C++.
Big Thanks! First of all, I’d like to thank all of the participants for sending the solutions.
There are many situations where you need to express that something is “optional” - an object that might contain a value or not. You have several options to implement such case, but with C++17 there’s probably the most helpful way: std::optional.
For today I’ve prepared one refactoring case where you can learn how to apply this new C++17 feature.
Writing a factory method might be simple:
unique_ptr<IType> create(name) { if (name == "Abc") return make_unique<AbcType>(); if (name == "Xyz") return make_unique<XyzType>(); if (...) return ... return nullptr; } Just one switch/if and then after a match you return a proper type.
But what if we don’t know all the types and names upfront?
Have you ever used the pimpl idiom in your code? No matter what’s your answer read on :)
In this article I’d like to gather all the essential information regarding this dependency breaking technique. We’ll discuss the implementation (const issue, back pointer, fast impl), pros and cons, alternatives and also show examples where is it used.
One of the key points of modern C++, as I observe, is to be expressive and use proper types. For example, regarding null pointers, rather than just writing a comment:
void Foo(int* pInt); // pInt cannot be null I should actually use not_null<int *> pInt.
The code looks great now, isn’t it?
Real life:
Fixed 1 out of 99 bugs in a project. 117 to go…
Have you experienced something similar? Although it’s impossible to write bug-free code, there are tools and practices to lower the rate of mistakes.
Today, I’d like to run through a gigantic list of freely available resources from the PVS-Studio Team who works with bugs analysis on a daily basis.
One of the guidelines from Modern C++ is to avoid using raw new and delete. Instead, you should use a smart pointer, a container or other RAII object. Today I’d like to focus on so-called ‘sink functions’ that takes ownership of input parameters. How can we modernize code around such calls?
Variadic Templates from C++11 is probably not a feature that you use on a daily basis. But recently, I’ve come across one refactoring example where I’ve decided to give a try and apply variadics.
Intro When I was doing some work in some old UI code I’ve noticed several similar lines of code that looked like that: