Modernize: Sink Functions

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?

READ MORE...

Const, Move and RVO

C++ is a surprising language. Sometimes simple things are not that simple in practice. Last time I argued that in function bodies const should be used most of the time. But two cases were missed: when moving and when returning a value. Does const influence move and RVO? Intro Just to recall, we’re talking here about using const for variables inside function bodies.

READ MORE...

Variadic Templates and a Factory Function

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:

READ MORE...

Wrapping Resource Handles in Smart Pointers

Some time ago I covered how to use custom deleters with smart pointers. The basic idea is to specify a dedicated method that will be called when a pointer is released. One logical application of custom deleters might be resource handles like files or the WinApi HANDLE type. Let’s see how can we implement such thing.

READ MORE...

Custom Deleters for C++ Smart Pointers

Let’s say we have the following code: LegacyList* pMyList = new LegacyList(); ... pMyList->ReleaseElements(); delete pMyList; In order to fully delete an object we need to do some additional action. How to make it more C++11? How to use unique_ptr or shared_ptr here? Intro We all know that smart pointers are really nice things and we should be using them instead of raw new and delete.

READ MORE...

C++ Status at the end of 2015

Maybe I’ll be boring with this note, but again I need to write that this was another good year for C++! Here’s a bunch of facts: Visual Studio 2015 was released with great support for C++14⁄17 and even more experimental features. Long-awaited GCC 5.0 was released at the beginning of the year.

READ MORE...

Auto keyword in C++11

// how does it work? auto i = 0; // ?? C++11 brings us a very useful set of tools. It adds fresh air to the hard life of a programmer. The updated syntax makes the language a more modern and easier to use. In this post let’s take a quick look at a nice keyword ‘auto’ that, at first sight might seem very simple.

READ MORE...

Errata and a Nice C++ Factory Implementation

I’ve finally got my copy of “Effective Modern C++”! The book looks great, good paper, nice font, colors… and of course the content :) While skimming through it for the first (or second) time I’ve found a nice idea for a factory method. I wanted to test it. The idea In the Item 18 there was described how to use std::unique_ptr and why it’s far better than raw pointers or (deprecated) auto_ptr.

READ MORE...

C++ Status at the end of 2014

This was a good year for C++! Short summary (language features): Clang supports C++14 GCC supports C++11 and most of C++14 (Full support in upcoming GCC 5.0) Intel 15.0 supports C++11 (some features on Linux/OSX only) Visual Studio tries to catch up with C++11, but it also introduces C++14 features as well… and it become (almost) free!

READ MORE...

Top 5 Beautiful C++ std Algorithms Examples

Some time ago I’ve seen an inspiring talk from CppCon 2013: “C++ Seasoning” by Sean Parent. One of the main points of this presentation was not to use raw loops. Instead, prefer to use existing algorithms or write functions that ‘wraps’ such loops. I was curious about this idea and searched for nice code examples.

READ MORE...

C++ status at the end of 2013

C++11 conformance GCC 4.81 - 100% Clang 3.3 - 100% Intel 14.0 - 84% Visual C++ 2013 - 66% Another year is almost over so it is a good time to check what is going on with C++. This time more stats and real data compared to my post from the previous year.

READ MORE...