Articles & Books

Guidelines For Rvalue References In APIs--Jonathan Müller

Everything you need to know.

Guidelines For Rvalue References In APIs

by Jonathan Müller

From the article:

I’ll be giving a talk at ACCU about when to use which pointer types and why.

While working on that I made some guidelines for rvalue references in interfaces which didn’t quite fit the talk, so I’m writing about them here.

When should you use rvalue references as function parameters?

When as return types?

What are ref-qualified member functions and when and how should you use them?

Let’s tackle it one by one...

My Little (String) Optimization, Part 2--Jordan Rose

Performance!

My Little (String) Optimization, Part 2

by Jordan Rose

From the article:

Previously, I talked about how Clang is smart enough to optimize a series of comparisons against constant strings in C++ by starting out with a switch on the length. I left off with the idea that while this is good, you might be able to do better if your strings have a unique character at a certain offset. Today we’re going to see what that looks like.

Freestanding trip report: emBO++ and Jacksonville wg21 2018 experience-Ben Craig

A good experience.

Freestanding trip report: emBO++ and Jacksonville wg21 2018 experience

by Ben Craig

From the article:

I'm the author of P0829, Freestanding Proposal. The tl;dr of the paper is that it standardizes a subset of the library suitable for kernel and embedded programming. R0 of this poorly titled paper was reasonably well received in the Albuquerque 2017 meeting. I was encouraged to send it out to a wider audience... and so I did. One of the people that I sent it to was Odin Holmes, and that got me an invitation to emBO++, my first speech at a public conference. This conference was the week prior to the Jacksonville meeting, so I ended up flying from Bochum to Jacksonville without going home first...

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