Sorting integer_sequence at compile time

The other day I was pointed at this excellent set of template-metaprogramming exercises:

The exercises build gradually, from Prepend and Append, to RemoveFirst and RemoveAll, to Sort. The only things I’d have done differently in that sequence are to include PopFront (which is easier) before PopBack (which, unless I’m missing something, is harder); and include Iota (i.e. std::make_index_sequence) after Length.

Read More

Feature requests for the Auto macro

From time to time readers send me “feature requests” for the Auto scope-guard macro (“The Auto macro” (2018-08-11)). Usually, I say “No need!” The neat thing about Auto’s particular syntax is that it’s conceptually just a way to defer code to the end of a scope. Feature requests usually take the form of modifying the deferred code in some way — which is already (and more explicitly) allowed simply by… writing the code that way.

Read More

Infinite Craft

I’m enjoying the heck out of Neal Agarwal’s web game Infinite Craft. This is a “crafting game” where you start with just four elements and repeatedly combine pairs of elements to create new elements — for example, Water plus Fire equals Steam. The clever gimmick is that instead of hard-coding a finite number of possible combinations, Infinite Craft’s backend (certainly looks as if it) simply asks an LLM what the result ought to be. So the combinations literally never run out.

Read More

A note on feature-test macros

Previously on this blog: “C++2a idioms for library feature detection” (2018-10-26).

Every C++ implementation is required to provide feature-test macros indicating which features it supports. These come in two flavors:

  • Core-language macros such as __cpp_generic_lambdas, specified in [cpp.predefined]. These are provided by the compiler vendor via the same mechanism as __FUNCTION__ and __GNUC__.

  • Library macros such as __cpp_lib_ranges_iota, specified in [version.syn]. These are provided by the library vendor via the same mechanism as assert and _GLIBCXX_RELEASE. They all begin with __cpp_lib_. The easiest way to get all of them at once is to #include <version> (since C++20).

Read More

Pentoprimality

Over on /r/askmath a couple days ago, “electricmaster23” posed the following puzzle:

Pack the 12 pentominoes into a 6x10 rectangle, then label each cell of that rectangle with a digit from 1 to 5 such that each pentomino contains all of 1 through 5, and, whenever two adjacent cells belong to different pentominoes, their labels’ sum is a prime number.

Read More

What I’m reading lately: Africa, liberty, economics

In December I reread Mike Resnick’s Galactic Comedy, a trilogy of histories of three sci-fi planets based on the 19th- and 20th-century histories of three different African nations. Paradise is based on Kenya (Selous, Jomo Kenyatta, Mau Mau); Purgatory on Zimbabwe (Cecil Rhodes, Kariba Dam, Second Matabele War, Great Zimbabwe); and Inferno on Uganda (Milton Obote, Idi Amin, expulsion of the Asians). I recommend basically everything Mike Resnick ever wrote.

Read More

Should assignment affect is_trivially_relocatable?

Consider the following piece of code that uses Bloomberg’s bsl::vector:

#include <bsl_vector.h>
using namespace BloombergLP::bslmf;

struct T1 {
    int i_;
    T1(int i) : i_(i) {}
    T1(const T1&) = default;
    void operator=(const T1&) { puts("Assigned"); }
    ~T1() = default;
};
static_assert(!IsBitwiseMoveable<T1>::value);

int main() {
    bsl::vector<T1> v = {1,2};
    v.erase(v.begin());
}

bsl::vector::erase uses T1::operator= to shift element 2 leftward into the position of element 1 (and then destroys the moved-from object in position 2). The program prints “Assigned.”

But that’s because type T1 is not trivially relocatable!

Read More

Holiday puzzle: Anagrams modulo two

At today’s “Gathering 4 Gardner” social, Gordon Lessells presented his annual Christmas puzzle. The mechanism, which he just started playing with a couple of weeks ago, is “cancelling pairs of duplicate letters.” For example, when you cancel each pair of duplicate letters in the word LESSELLS, you throw out EE, LL, and SS and are left with LS. If you cancel each pair of duplicates in TENNESSEE, you’re left with simply T.

Read More

A Dissertation Upon Roast Pig (1822)

Happy Thanksgiving! I haven’t got a reading on roast turkey, but here are some excerpts from Charles Lamb’s “A Dissertation Upon Roast Pig” (London Magazine, September 1822):

I am one of those who freely and ungrudgingly impart a share of the good things of this life which fall to their lot (few as mine are in this kind) to a friend. I protest I take as great an interest in my friend’s pleasures, his relishes and proper satisfactions, as in mine own. “Presents,” I often say “endear Absents.” Hares, pheasants, partridges, snipes, barn-door chickens (those “tame villatic fowl”), capons, plovers, brawn, barrels of oysters, I dispense as freely as I receive them. I love to taste them, as it were, upon the tongue of my friend.

But a stop must be put somewhere. One would not, like Lear, “give everything.” I make my stand upon pig.

Read More

My muscle-memory git toolbox

This blog post has been on my to-do list ever since I saw Daniel Stenberg’s blog post “This is how I git” (November 2020). I thought, “I should do one of those posts, too!”

This post focuses on my basic muscle-memory git commands. There are at least two other major Git subtopics this post doesn’t mention at all: “branching discipline” (what is a release branch? what’s the difference between rebase and merge?) and “hygiene” (how big should a commit be? what does a good commit message look like?). That is — as usual for this blog — we’re talking tactics, not strategy.

Read More