Partners: KDAB Whole Tomato Software

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.

27 November 2017

Summary of C++17 features

Summary of C++17 features

How do you see the new C++ standard? Is it ok? Great? Meh?

Last week, after a few years of break, I presented my new talk that addressed the above question! It happened at the Cracow C++ Local Group.

Have a look what’s inside this talk.

20 November 2017

code::dive 2017 conference report

Code::Dive 2017 report

This year I had a chance to visit Wroclaw for the 4th edition of Code Dive! Two days of a great programming conference!

Briefly: many of topics related to C++, two days, 40 presentations, lots of people and a beautiful city. What more do you need? :)

Continue below to read my report from the event.

06 November 2017

Enforcing code contracts with [[nodiscard]]

nodiscard attribute C++17

For my article series about C++17 features, I’ve made a separate entry about new attributes. At first sight, I thought that [[nodiscard]] is just another simple and a rarely used thing. But later I thought… hmmm… maybe it might be valuable?

One reason is that [[nodiscard]] might be handy when enforcing code contracts. That way you won’t miss an important return value.

Let’s look at a few examples.

30 October 2017

Better code understanding with Sourcetrail

Sourcetrail, review

Do you write code 100% of your job time?

I’m guessing the answer is no. Ignoring the whole management part, meetings, coffee, youtube, cats, etc, even if you’re sitting at your keyboard you’re not typing all the time. So what else are you doing?

The short answer: you’re probably figuring out what to write and where to add new features (and what to fix)… so you’re mostly reading and trying to understand the code.

There are tools to help you better understand code which are not just text editors. One of such application is Sourcetrail, let’s see what can it do for us.

Bonus!, read further to see details of our new giveaway :)

© 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.