Inheritance is a handy tool to simplify design and avoid code duplication. However, good old fashion inheritance is not always the right tool for the job. What if a base class could know, at compile time, who derives from it? Enter the curiously recurring template pattern (CRTP)...
call_me_maybe function takes a target as its only argument. If the target object can be called with some specific arguments, it will call it. But not immediately —it doesn't want to look desperate—; instead, it will store it somewhere until the time is right. What follows is how such a crazy function is implemented...
There are 5 types of character literals in C++. Two types of character literal for the narrow-kings under the sky, two for the universal-lords in their halls of stone, one for the mortal wide doomed to die, in the land of C++ where the shadows lie.
In the beginning there was no
bool. And C++ said "let there be
bool", and there was
The Boolean data type is an integral type with only two values:
false, intended to represent the truth values of logic and Boolean algebra. With only two values, what can possibly go wrong?
A customization point is a code construct that the user can leverage to specialize how a particular library action is handled. A common way of implementing this in C++ is to define a template with the default behavior, and let users specialize it for their own types —e.g.,
std::hash—. This is the story of Spirit X3 and how it lets you specialize customization points without ever leaving your own namespace, sort of...
The Zen of Python tell us that Explicit is better than Implicit. This is good advice for any and all languages, but what are the implications in the C++ lands? There is one particular C++ feature that relates directly to that advice; one important enough that grants on its own the introduction of a keyword. That feature is user-defined conversions and that keyword is
C++'s basic source character set consists of only 96 characters, while also offering a way to name any character in the ISO10646 universal character set —the character repertoire of Unicode—. As little as this may sound, nine of those characters lay outside the ISO646 invariant character set. This can be problematic when the encoding and/or keyboard used to write code does not support one or more of these nine characters. Hence, a workaround is born...
Episode Three tells us about friendship, and how it can be used to narrow access to a class or function. However, that's not the only thing friendship restricts, it also restricts how we may use such class or function. What follows is a concrete example of how friendship may cramp your style.