Using C++17 std::optional

Let’s take a pair of two types <YourType, bool> - what can you do with such composition? In this article, I’ll describe std:optional - a new helper type added in C++17. It’s a wrapper for your type and a flag that indicates if the value is initialized or not. Let’s see where it can be useful and how you can use it.

READ MORE...

Refactoring with C++17 std::optional

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.

READ MORE...

Productive C++ Developer, my recent talk

A few weeks ago I gave another talk at my local C++ user group. We discussed recent “goodies” from C++ and tools that can increase productivity. Intro In my post for the “C++ summary at the end of 2017” I mentioned that we could see a considerable improvement in the area of tooling for the language.

READ MORE...

Deprecating Raw Pointers in C++20

The C++ Standard moves at a fast pace. Probably, not all developers caught up with C++11⁄14 yet and recently we got C++17. Now it’ time to prepare C++20! A few weeks ago The C++ Committee had an official ISO meeting in Jacksonville, FL (12-17 March 2018) where they worked hard on the new specification.

READ MORE...

The C++ Standard Library book - overview & giveaway

Let’s have a quick overview of another book related to Modern C++ and The Standard Library. This time I picked Rainer Grimm’s book the author of the modernescpp blog. Read more if you’d like to win C++ book bundle! :) The book The C++ Standard Library What every professional C++ programmer should know about the C++ standard library

READ MORE...

Simplify code with 'if constexpr' in C++17

Before C++17 we had a few, quite ugly looking, ways to write static if (if that works at compile time) in C++: you could use tag dispatching or SFINAE (for example via std::enable_if). Fortunately, that’s changed, and we can now take benefit of if constexpr! Let’s see how we can use it and replace some std::enable_if code.

READ MORE...

Factory With Self-Registering Types

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?

READ MORE...

How to propagate const on a pointer data member?

Inside const methods all member pointers become constant pointers. However sometimes it would be more practical to have constant pointers to constant objects. So how can we propagate such constness? The problem Let’s discuss a simple class that keeps a pointer to another class. This member field might be an observing (raw) pointer, or some smart pointer.

READ MORE...

The Pimpl Pattern - what you should know

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.

READ MORE...