Articles & Books

Give Your Old C/C++ Code Some New Love -- Richard Thomson

This article explores how to modernize your legacy C/C++ code base, using an open source fractal renderer originally written in C and x86 assembly language as the case study.

Give Your Old Code Some New Love

by Richard Thomson

From the article:

If you’re not careful, entropy creeps its way into your code base. You take a shortcut or code something in a way that you know is sloppy and you say to yourself “I’ll come back to that later”, but later you’re faced with new feature requests or some other Imminent Disaster(tm) and you don’t go back and clean up the mess you made earlier. This is only natural in a code base and when the messes are few and far between, it is tolerable.

[...]

In this post, we’ll take a look at an open source project with a code base that is over 30 years old and has accumulated some “cruft” along the way. We’ll discuss various strategies for coping with the cruft and how to get rid of it in as safe a manner as possible. After all, we don’t want to introduce bugs while we clean up cruft.

The “unsigned for value range” antipattern--Arthur O’Dwyer

What do you think?

The “unsigned for value range” antipattern

by Arthur O’Dwyer

From the article:

Background: Signed Integers Are (Not Yet) Two’s Complement
At the WG21 committee meeting which is currently underway in Jacksonville, JF Bastien will be presenting a proposal to make C++’s int data type wrap around on overflow. That is, where today the expression INT_MAX + 1 has undefined behavior, JF would like to see that expression formally defined to come out equal to INT_MIN...

String’s competing constructors--Andrzej Krzemieński

A tough problem.

String’s competing constructors

by Andrzej Krzemieński

From the article:

Let’s start with the problem. I want to check whether a program received a text message that consists of four consecutive zeroes. Not '0', but the numeric zero. I will create a constant std::string representing the special sequence and compare the messages (also stored as std::strings) I receive...

Simplify code with 'if constexpr' in C++17--Bartlomiej Filipek

With examples.

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

by Bartlomiej Filipek

From the article:

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.

Quick Q: What is a non-trivial constructor in C++?

Quick A: All constructors that you define are not trivial.

Recently on SO:

What is a non-trivial constructor in C++?

In simple words a "trivial" special member function literally means a member function that does its job in a very straightforward manner. The "straightforward manner" means different thing for different kinds of special member functions.

For a default constructor and destructor being "trivial" means literally "do nothing at all". For copy-constructor and copy-assignment operator, being "trivial" means literally "be equivalent to simple raw memory copying" (like copy with memcpy).

If you define a constructor yourself, it is considered non-trivial, even if it doesn't do anything, so a trivial constructor must be implicitly defined by the compiler.

In order for a special member function to satisfy the above requirements, the class must have a very simplistic structure, it must not require any hidden initializations when an object is being created or destroyed, or any hidden additional internal manipulations when it is being copied.

For example, if class has virtual functions, it will require some extra hidden initializations when objects of this class are being created (initialize virtual method table and such), so the constructor for this class will not qualify as trivial.

For another example, if a class has virtual base classes, then each object of this class might contain hidden pointers that point to other parts of the very same object. Such a self-referential object cannot be copied by a simple raw memory copy routine (like memcpy). Extra manipulations will be necessary to properly re-initialize the hidden pointers in the copy. For this reason the copy constructor and copy-assignment operator for this class will not qualify as trivial.

For obvious reasons, this requirement is recursive: all subobjects of the class (bases and non-static members) must also have trivial constructors.

A Foolish Consistency--Jon Kalb

A very interesting article that we should all read.

A Foolish Consistency

by Jon Kalb

From the article:

Ralph Waldo Emerson famously said, “A foolish consistency is the hobgoblin of little minds, adored by little statesmen and philosophers and divines.” I don’t think he was talking about code, but that statement couldn’t be more relevant to software engineers.

I’ve experienced a scenario like this a number of time in my career:

I’m sharing a new approach to writing code that offers some clear improvements to what we’ve been doing. Perhaps it is more readable, more efficient, or safer. But the response that I hear from colleagues is, “But we can’t do that here. We have <some large number> lines of code where we didn’t do it that way, so it wouldn’t be consistent.”

C++17 Features, in easy to understand before/after tables -- Tony Van Eerd

Results of a recent survey suggest that many devs have a hard time keeping up with changes between each rev of C++.

C++17 before and after table

by Tony Van Eerd

About the article:

Here's a collection of the major changes between C++14 and C++17, with example code for each - typically showing how it may have been written before C++17, and how it can now be written with the new C++17 feature(s).

 

 

C++ Rename Shootout: Visual Studio 2017 vs. ReSharper for C++ -- Richard Thomson

A comparison of two automatic rename tools for C++ in Visual Studio.

C++ Rename Shootout: Visual Studio 2017 vs. ReSharper for C++

by Richard Thomson

About the post:

Getting help from your development environment for renaming identifiers can be a huge productivity boost. It lets you quickly and easily improve the names of things in your code. In this post, I’ll review two automatic renaming tools for C++ in Visual Studio: Visual Studio 2017 Community Edition 15.5.7 and ReSharper for C++ 2017.3.2.

Selective argument application -- Krzysztof Ostrowski

Presentation of a technique that enables transparent interception of the selected arguments passed to any function by means of a user-provided predicate metafunction.

Selective argument application

by Krzysztof Ostrowski

From the article:

Presentation of a design of an interceptor abstraction built over C++17 utilities that records values of the selected arguments passed to any action transparently, then applies that action to the captured arguments, and eventually passes the result value to the next action.

RIP index_sequence, 2014–2017 -- Matt Aubury

Matt Aubury philosophizes about digging a grave for certain std::index_sequence idioms. 

RIP index_sequence, 2014–2017

by Matt Aubury

From the article:

std::tuple was one of the great additions to C++11. Whilst sometimes abused by lazy programmers (who should really be using a struct or class), it’s true value is as a container of arbitrary values in variadic templates.

Sadly, static typing makes working with tuples much harder in C++ than in most languages [...]