Adventure 448 (SWEN0448): All the easter eggs
This week Jason Dyer played Adventure 448, in a couple of blog posts:
…
Arthur O’Dwyer
Stuff mostly about C++
Adventure 448 (SWEN0448): All the easter eggs
This week Jason Dyer played Adventure 448, in a couple of blog posts:
…
When your loop unroller works harder than your infinite loop detector
…
Homeworlds: Analysis of an Opening Gambit
This post is about Binary Homeworlds, the “space chess” game invented by John Cooper and popularized by (among others) Andy Looney. You can play Homeworlds online via email at SuperDuperGames, and, as of this year, in real time at Homeworlds Live. Jonathan Baker runs an annual tournament, which is happening as we speak.
…
A hole in Clang’s -Wsuggest-override
Consider this code:
struct B {
virtual int f() = 0;
virtual int g() = 0;
};
struct C : B {
int f() override;
virtual int g() final; // overrides
virtual int h() final; // doesn't
}; ...
When can the C++ compiler devirtualize a call?
Someone recently asked me about devirtualization optimizations: when do they happen?
when can we rely on devirtualization? do different compilers do devirtualization
differently? As usual, this led me down an experimental rabbit-hole. The answer
seems to be: Modern compilers devirtualize calls to final
methods pretty reliably.
But there are many interesting corner cases — including some I haven’t thought of,
I’m sure! — and different compilers do catch different subsets of those corner cases.
…
Blog roundup: Auftragstaktik, files, lesser-known cryptocurrencies
Once again I’ve fallen down the very deep rabbit hole of Bret Devereaux’s blog A Collection of Unmitigated Pedantry. (Previously on this blog: “What I’m reading lately” (2020-07-06).) This time it’s a multi-part series on the myth of the “universal experience of war”:
…
decltype
of a non-static member
Today I learned ([expr.prim.id]/3):
…
What are X-macros?
“X-macros” is a neat C and C++ technique that doesn’t get enough advertisement. Here’s the basic idea:
Suppose we have a table of similar records, each with the same schema. In games, this might be our collection of monster types (each with a display name, a representative icon, a dungeon level, a bitmask of special attack types, etc). In networking, this might be our collection of error codes (each with an integer value, a message string, etc).
We could encode that information into a data structure that we traverse at runtime to produce interesting effects — for example, an array of structs that we index into or loop over to answer a question like “What is the error string for this enumerator?” or “How many monsters have dungeon level 3?”.
But the “X-macros” technique is to encode that information in source code, which can be manipulated at compile time. We encode the information generically, without worrying about how it might be “stored” at runtime, because we’re not going to store it — it’s just source code! …
Fun with conversion-operator name lookup
As of this writing (but perhaps not for very much longer!) the four mainstream compilers on Godbolt Compiler Explorer give four different answers for this simple C++ program:
…
It’s not always obvious when tail-call optimization is allowed
I initially wrote this as part of a new entry in “A C++ acronym glossary” (2019-08-02), but decided that it would be better to pull it out into its own post.
…
Thoughts on “Does GPT-2 Know Your Phone Number?”
Via Hacker News: “Does GPT-2 Know Your Phone Number?” (Eric Wallace, Florian Tramèr, Matthew Jagielski, and Ariel Herbert-Voss, December 2020).
…
The Box of Delights
I’ve just finished reading John Masefield’s The Box of Delights (1935). I had picked it up at random in a tourist-trap gift shop several months ago, knowing only that it appeared vaguely Christmassy — not knowing at the time that it is (apparently) a giant of British Christmas pop culture, on par with The Nutcracker.
…
A better 404 polyglot
Between 2009-ish and 2018, StackOverflow’s 404 page displayed the following polyglot program:
# define v putchar
# define print(x) ⏎
main(){v(4+v(v(52)-4));return 0;}/*
#>+++++++4+[>++++++<-]>⏎
++++.----.++++.*/
print(202*2);exit();
#define/*>.@*/exit()
This program was originally due to Mark Rushakoff, who later proposed a shorter version: …
How do C++ using-directives work?
Recall that in C++ there’s a difference between a using-declaration and a using-directive.
…
The STL is more than std::accumulate
Conor Hoekstra gives great talks on algorithms. Notably, “Algorithm Intuition” (C++Now 2019) and “Better Algorithm Intuition” (code::dive 2019). However, every time I watch one of his talks where he uses STL algorithms to solve some programming problem, I come away feeling like
Let’s look at two examples of “using STL algorithms to solve problems” in this limited sense, and how else we might solve them. Both of these examples are taken from Conor’s CppCon 2020 talk “Structure and Interpretation of Computer Programs: SICP.” …
Bubblesort, rocksort, and cocktail-shaker sort
I’m currently participating in a book club for Donald Knuth’s The Art of Computer Programming. It’s run by Zartaj Majeed; you can find the details on Meetup here.
…
Tips re operator()
Several times this week I’ve seen people talking about C++20’s new generic lambdas with explicit template parameters. Time for a blog post so I can link to it…
…
“Flash of unstyled base class”
This week I ran into an interesting bug in the unit tests for some multithreaded code. The setup for the test involved a “process status daemon” that (in production) polled some global state once per second, and also provided some auxiliary functions that we wanted to test. The “once per second” code came from an old and widely-used internal helper library. In this retelling, I’ve taken the liberty of updating it for C++20:
…
An example of the Barton–Nackman trick
I still need to write the blog post explaining “What is the Hidden Friend Idiom?”; but for now, here’s a practical demonstration of its usefulness that just came up on the cpplang Slack (thanks, Johan Lundberg!).
…
Loop variable may not be declared static
…