Partners: KDAB Whole Tomato Software CppDepend

23 April 2018

Refactoring with C++17 std::optional

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.

09 April 2018

Productive C++ Developer, my recent talk

C++ Goodies and tools

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.

01 April 2018

Deprecating Raw Pointers in C++20

Raw pointers in C++

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.

Besides many significant things that were discussed at the meeting like modules, concepts, ranges, The C++ Committee accepted one hugely anticipated feature: deprecation of raw pointers!

26 March 2018

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! :)

12 March 2018

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

Simplify your 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.

26 February 2018

What happens to your static variables at the start of the program?

Static Variables in C++

Saying that C++ has simple rules for variables initialization is probably quite risky :) For example, you can read Initialization in C++ is Bonkers : r/cpp to see a vibrant discussion about this topic.

But let’s try with just a small part of variables: static variables.
How are they initialized? What happens before main()(*) ?

12 February 2018

Static Variables Initialization in a Static Library, Example

C++ static variables initialization and linker, one example

This post is motivated by one important comment from my last article about factories and self-registering types:

(me) So the compiler won’t optimize such variable.

Yet, unfortunately, the linker will happily ignore it if linking from a static library.

So… what’s the problem with the linker?

05 February 2018

Factory With Self-Registering Types

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? Or when we’d like to make such factory more generic?

Let’s see how classes can register themselves in a factory and what are the examples where it’s used.

22 January 2018

How to propagate const on a pointer data member?

propagate_const, C++

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?

15 January 2018

pimpl vs Abstract Interface - a practical tutorial

Pimpl vs abstract interface

Let’s see pimpl and its alternatives in a real application! I’ve implemented a small utility app - for file compression - where we can experiment with various designs.

Is it better to use pimpl or maybe abstract interfaces? Read on to discover.

08 January 2018

The Pimpl Pattern - what you should know

PIMPL pattern

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. You’ll also see how modern C++ can change this pattern. Moreover, I hope you’ll help me and provide your examples.

31 December 2017

C++ Status at the end of 2017

C++ Status at the end of 2017

In Poland, it’s only a few hours until the end of the year, so it’s an excellent chance to make a summary of things that happened to C++! As you might guess the whole year was dominated by the finalization and publication of C++17. Yet, there are some other “big” things that happened. Let’s see the full report.

11 December 2017

How a weak_ptr might prevent full memory cleanup of managed object

Weak pointer and shared pointer

When I was working on the Smart Pointer Reference Card I run into a quite interesting issue. It appears that in some cases memory allocated for the object controlled by smart_ptr might not be released until all weak pointers are also ‘dead’.

Such case was surprising to me because I thought that the moment the last share_ptr goes down, the memory is released.

Let’s drill down the case. It might be interesting as we’ll learn how shared/weak pointers interact with each other.

04 December 2017

5 ways how unique_ptr enhances resource safety in your code


Modern C++ stresses the use of RAII objects to manage resources. One of the easiest ways is just to start using unique_ptr across your code.

Let’s see how we can leverage this smart pointer type. I’ve come up with 5 (or more?) reasons where unique_ptr shines.

© 2017, Bartlomiej Filipek, Blogger platform
Any opinions expressed herein are in no way representative of those of my employers.
This site contains ads or referral links, which provide me with a commission. Thank you for your understanding.