Articles & Books

Type annotation in C++--Stoyan Nikolov

How do you do it?

Type annotation in C++

by Stoyan Nikolov

From the article:

In systems like game engines and our HTML renderer Hummingbird, developers have to work with objects transformed in different coordinate systems. Using one generic type can lead to confusion on what object is required in a particular situation. Errors are often subtle and hard to track. I tried to mitigate this by using stringent static typing in our software. New types are created by annotating them with metadata...

Auto Type Deduction in Range-Based For Loops--Petr Zemek

Which one to use?

Auto Type Deduction in Range-Based For Loops

by Petr Zemek

From the article:

Have you ever wondered which of the following variants you should use in range-based for loops and when? auto, const auto, auto&, const auto&, auto&&, const auto&&, or decltype(auto)? This post tries to present rules of thumb that you can use in day-to-day coding. As you will see, only four of these variants are generally useful.

Great Expectations--Glennan Carnie

Let's review the basics!

Great Expectations

by Glennan Carnie

From the article:

Previously, we’ve looked at the basic concepts of function parameter passing, and we’ve looked at the mechanics of how parameters are passed at the Application Binary Interface (ABI) level.

Far too often we focus on the mechanisms and efficiency of parameter passing, with the goal: if it’s efficient then it’s good; that’s all there is to it.  In this article I want to move past simple mechanics and start to explore function parameter design intent – that is, what can I expect (to change) about the objects I use as function arguments; and what can I expect to be able to do with an object as a function implementer.

To that end, we’ll take a look at parameter passing from the perspective of the mutability (ability to be modified) of the parameters from both a caller’s and a function’s (callee’s) point of view...

awesome-cmake released! -- Viktor Kirilov

Even cmake is not about C++, it is the most popular platform independend make tool used for C++.

awesome-cmake Released!

by Viktor Kirilov

From the collection:

This curated list contains awesome CMake scripts, modules, examples, and others.

Quick Q: Marking std::unique_ptr class member as const

Quick A: It prevents you of being able to move your class.

Recently on SO:

Marking std::unique_ptr class member as const

Because of the nature of a std::unique_ptr (sole ownership of an object) it's required to have no copy constructor whatsoever. The move constructor(6) only takes non-const rvalue-references which means that if you'd try to make your _child const and move it you'd get a nice compilation error smile

Even if a custom unique_ptr would take a const rvalue-reference it would be impossible to implement.

Quick Q: Is the std::array bit compatible with the old C array?

Quick A: Yes, you can copy bitwisely from one to the other.

Recently on SO:

Is the std::array bit compatible with the old C array?

The requirement on the data() method is that it return a pointer T* such that:

[data(), data() + size()) is a valid range, and data() == addressof(front()).

This implies that you can access each element sequentially via the data() pointer, and so if T is trivially copyable you can indeed use memcpy to copy sizeof(T) * size() bytes to/from an array T[size()], since this is equivalent to memcpying each element individually.

However, you cannot use reinterpret_cast, since that would violate strict aliasing, as data() is not required to actually be backed by an array - and also, even if you were to guarantee that std::array contains an array, since C++17 you cannot (even using reinterpret_cast) cast a pointer to an array to/from a pointer to its first member (you have to use std::launder).

Episode Eleven: To Kill a Move Constructor--Agustín "K-ballo" Bergé and Howard Hinnant

Following here is an advanced article about the behavior of C++ concerning copy and move operations. A more simple version is provided for a quicker and easier understanding of the best practices. The sum up is, don't declare as deleted a move constructor!

A more user friendly version

by Howard Hinnant

Episode Eleven: To Kill a Move Constructor

by Agustín "K-ballo" Bergé

From the article:

Unlike copy operations, which are provided by the compiler if not user declared, move operations can and often are suppressed such that a class might not have one. Furthermore, it is possible for a class to have a —user declared— move operation which is both defined as deleted, and at the same time ignored by overload resolution, as if it didn't exist...

 

Top 15 C++ Exception handling mistakes and how to avoid them--Deb Haldar

This article changed my vision about exceptions:

Top 15 C++ Exception handling mistakes and how to avoid them

by Deb Haldar

From the article:

Do you use exception handling in your C++ code?

If you don’t, why not?

Perhaps you’ve been conditioned to believe that exception handling is bad practice in C++. Or maybe you think that it’s prohibitively expensive in terms of performance. Or maybe it’s just not the way your legacy code is laid out and you’re stuck in the rut.

Whatever your reason is, it’s probably worth noting that using C++ Exceptions instead of error codes has a lot of advantages. So unless you’re coding some real-time or embedded systems, C++ exceptions can make your code more robust, maintainable and performant in the normal code path (yes performant, you read that right !).

In this article we’re going to look at 15 mistakes that a lot of developers make when just stating off with C++ exceptions or considering using C++ exceptions...

How C++14 and C++17 help to write faster (and better) code. Real world examples -- Dan Levin

High-performance code and new standards of C++ with code examples.

How C++14 and C++17 help to write faster (and better) code. Real world examples

by Dan Levin

From the article:

Writing high performance code is always a difficult task. Pure algorithms are not always a good fit for the real world architectures.

Often, we’re limited to just two options: either beautiful and slow or fast and ugly.

In this article I’ll show when C++11 and C++14 can help to write fast, compact and well-structured code.