Arthur O’Dwyer
224 FOLLOWERS
Stuff mostly about C++. I started this blog on 2018-03-13 so that I'd have a place to dump short essays on topics that seem to come up a lot in the discussion about C++. I might occasionally say things that aren't about C++, too.
Arthur O’Dwyer
1w ago
At today’s Gathering 4 Gardner social, Colm Mulcahy presented on two Irish figures in recreational mathematics with whom Martin Gardner corresponded: Victor Meally and Owen O’Shea. Owen O’Shea is the natural successor to Gardner’s “Professor I.J. Matrix” as a prolific generator of numerological coincidences — see for example The Magic Numbers of the Professor (2007). Victor Meally shows up occasionally in Gardner’s Mathematical Games columns, and also in “Problem 3.14” (appropriate for Pi Day!) in The Colossal Book of Short Puzzles and Problems (2006):
One of the satisfactions of recreational ..read more
Arthur O’Dwyer
3w ago
As of late February, Google Summer of Code (GSoC) has published its official list of sponsoring open-source organizations, and each organization has independently published its own list of project ideas. People interested in doing a GSoC project (sort of a summer internship for open source software) should submit a proposal application — the quite short application window is March 18 to April 2.
If you’re a fan of my “trivial relocatability” content, or just want to help relocation’s progress into the mainstream of C++, you might be particularly interested in these three GSoC sponsors:
Ste ..read more
Arthur O’Dwyer
3w ago
Last month I wrote about Neal Agarwal’s web game Infinite Craft. Tom Fang wrote to tell me he’s created a dictionary of Infinite Craft elements, along with their uses and recipes. This got me thinking about the game’s mathematical structure.
By “mathematical structure,” I mean something like how we make recipes and the metrics by which we might compare one recipe to another. For example, if our goal is to make Sandwich, we could do it like this:
Wave = Water + Wind
Steam = Fire + Water
Plant = Earth + Water
Sand = Earth + Wave
Tea = Plant + Steam
Sandwich = Sand + Tea
Or like this:
Wave ..read more
Arthur O’Dwyer
3w ago
Yesterday, in “Sorting integer_sequence at compile time” (2024-02-19), I wrote:
I don’t know any good way to operate on a whole pack of Is... in value-space and then “return” them into type-space as Vector<Is...>.
So I had written a helper function to get each individual element of the result one by one, like this:
template<class> struct Sort;
template<int... Is> struct Sort<Vector<Is...>> {
static constexpr int GetNthElement(int i) {
int arr[] = {Is...};
std::ranges::sort(arr);
return arr[i];
}
template<class> struct Helper;
template ..read more
Arthur O’Dwyer
1M ago
The other day I was pointed at this excellent set of template-metaprogramming exercises:
“C++ Metaprogramming Exercises Vol. I” (Ondřej Slámečka, March 2021)
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.
Now, one way to implement Sort is to implement a selection sort from scratch by combining Min, RemoveFirst, and Prep ..read more
Arthur O’Dwyer
1M ago
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.
Before we look at those “not-a-bug” examples, let’s see the one feature request I’ve actually agreed with and adopted:
Throwing from deferred code
“My deferred code mig ..read more
Arthur O’Dwyer
1M ago
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.
Here’s a partial map of the starting elements’ neighborhood:
The game’s UI is quite different on mobile versus desktop ..read more
Arthur O’Dwyer
1M ago
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_ ..read more
Arthur O’Dwyer
2M ago
Someone on the cpplang Slack asks: Why can I give a non-capturing lambda a C++23 “deducing this” explicit object parameter of an arbitrary type, but I can’t do the same for a capturing lambda?
auto alpha = [](this std::any a) { return 42; };
// OK
auto beta = [&](this std::any a) { return 42; };
// ill-formed
GCC complains: “a lambda with captures may not have an explicit object parameter of an unrelated type.” (GCC’s diagnostic is — I think properly — SFINAE-friendly. Clang and MSVC — I think improperly — allow you to form the lambda type, and then error on any attempt to call it ..read more
Arthur O’Dwyer
2M ago
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.
“marpocky” pointed out that the puzzle was unsolvable, because of the X pentomino: no matter how you label its legs, you arrive at a contradiction. For example, if two adjacent legs of the X are labeled 3 and 4, then we must have \(x\) such that ..read more