Articles & Books

Quick Q; Concise explanation of reference collapsing rules requested: (1) A& & -> A&...

Quick A: the result is the same as calling the function you're forwarding to directly.

Recently on SO:

Concise explanation of reference collapsing rules requested: (1) A& & -> A& , (2) A& && -> A& , (3) A&& & -> A& , and (4) A&& && -> A&&

The reference collapsing rules (save for A& & -> A&, which is C++98/03) exist for one reason: to allow perfect forwarding to work...

Use constexpr for faster, smaller, and safer code--Trail of Bits

More safety, maybe more speed.

Use constexpr for faster, smaller, and safer code

by Trail of Bits

From the article:

With the release of C++14, the standards committee strengthened one of the coolest modern features of C++: constexpr. Now, C++ developers can write constant expressions and force their evaluation at compile-time, rather than at every invocation by users. This results in faster execution, smaller executables and, surprisingly, safer code...

Simplify Your Code With Rocket Science: C++20’s Spaceship Operator--Cameron DaCamara

Exited?

Simplify Your Code With Rocket Science: C++20’s Spaceship Operator

by Cameron DaCamara

From the article:

C++20 adds a new operator, affectionately dubbed the “spaceship” operator: <=>. There was a post awhile back by our very own Simon Brand detailing some information regarding this new operator along with some conceptual information about what it is and does.  The goal of this post is to explore some concrete applications of this strange new operator and its associated counterpart, the operator== (yes it has been changed, for the better!), all while providing some guidelines for its use in everyday code.

The Power of Hidden Friends in C++--Anthony Williams

Did you know about them?

The Power of Hidden Friends in C++

by Anthony Williams

From the article:

"Friendship" in C++ is commonly thought of as a means of allowing non-member functions and other classes to access the private data of a class. This might be done to allow symmetric conversions on non-member comparison operators, or allow a factory class exclusive access to the constructor of a class, or any number of things.

However, this is not the only use of friendship in C++, as there is an additional property to declaring a function or function template a friend: the friend function is now available to be found via Argument-Dependent Lookup (ADL). This is what makes operator overloading work with classes in different namespaces...

Space Game: A std::variant-Based State Machine by Example--Nikolai Wuttke

Did you think to use it like that?

Space Game: A std::variant-Based State Machine by Example

by Nikolai Wuttke

From the article:

One of a powerful uses of std::variant is to implement State Machines. Some time ago I showed a simple example, but today we have something bigger. In today’s article by Nikolai Wuttke you’ll see how to leverage std::variant and build a space game!