How to erase from an STL container

C++20 introduces new library functions std::erase and std::erase_if. Notice that I said “functions,” not “algorithms”: these are not implemented as generic function templates, but rather as a closed set of function overloads, scattered across the STL’s many container-related headers.

Why are these new C++20 functions implemented as a massive overload set, instead of as a single “generic programming”–style function template? …

Read More

What I’m reading lately: ACOUP, Kitbashed, the Atlantic

Military historian Bret Devereaux has a blog called A Collection of Unmitigated Pedantry where he writes in-depth — very in-depth — dissections of pop antiquity such as Lord of the Rings, Game of Thrones, and Total War.

His “The Practical Case on Why We Need the Humanities” (July 2020) is perhaps the most morally important thing I have to recommend in this post, so I’m putting it first. But, when you’re done with that essay, check out:

  • “The Siege of Gondor” (May 2019; parts I, II, III, IV, V, VI) …
Read More

The Case of Willie the Wisp

As a kid, I’d sometimes read a story where one line has stuck with me over the years as the perfect expression of some archetype. I find myself wishing that other people knew these same stories, so that I could actually use these lovely shorthands.

Read More

Folding over operator=

Jonathan Müller posted a “Nifty Fold Expression Trick” the other day:

template<class F, class... Ts>
void reverse_for_each(F f, Ts... ts) {
    int dummy;
    (dummy = ... = (f(ts), 0));

For example, reverse_for_each(putchar, 'a', 'b', 'c') prints cba.

However, as I puzzled out each step of the process, I realized that there were several subtleties to this “simple” expression! …

Read More

Connect Fifteen

Here’s a simple two-player card game you can play while stuck at home. I invented it sometime before 2003 — I would guess circa 1999. I call it “Connect Fifteen.”

Read More

Default function arguments are the devil

If you frequently talk with me about C++, you have undoubtedly heard me say:

Default function arguments are the devil.

My position opposes the historical (early-1990s) style of the STL, of course; but I was recently alerted that it’s also in direct conflict with the (2010s) C++ Core Guidelines, specifically rule F.51. Since this conflict might, if unaddressed, lead people to think I’m wrong, I’d better address it. ;) …

Read More