Is path convertible to string_view?: a war story

This story comes from the libc++ review implementing P1989 “Range constructor for std::string_view 2: Constrain Harder” (Corentin Jabot, March 2021). That paper made std::string_view implicitly convertible-from basically any contiguous range of characters. Which, to be clear, is probably a good thing. But it turned up a really interesting interaction in libc++’s filesystem::path. Somehow, enabling this constructor template in the <string_view> header caused libc++’s filesystem::path to stop being a range!

Read More

A footnote on “Three reasons to pass std::string_view by value”

Several readers have responded to my recent post “Three reasons to pass std::string_view by value” (2021-11-09), pointing out that (while everything I said is true of the Itanium x86-64 ABI used on Linux and Mac) unfortunately Microsoft’s x86-64 ABI does not pass std::string_view in registers, but instead passes it by “hidden pointer,” so that at the machine level, you get basically the same codegen as if you had been passing by reference all along. Except that actually you’re passing a copy by reference, so you get one extra 16-byte copy in addition to all the other stack traffic MSVC is doing.

This is a big problem baked into Microsoft’s x86-64 ABI, and nerfs a lot of the performance benefits mentioned in my post. However, my advice remains the same: Even if your primary platform today is Windows, you should habitually pass std::string_view by value!

Read More

Image rotation by shifting pixels

Via Hacker News: You may know that you can reverse a string by first reversing the order of the words in the string, then reversing the characters in each word. (So hello world becomes world hello, and then we recursively reverse each word to get dlrow olleh.) Well, it turns out you can do the same kind of thing in two dimensions: to rotate an image by 90 degrees, you can just rotate the order of the quadrants, and then recursively rotate each quadrant.

Read More

Overload arrangement puzzles

I contributed some puzzles to the online venue for CppCon 2021. Some of my contributions were just C++ Pub Quiz questions recycled from 2019; but I also contributed a set of logic puzzles that I think are fairly novel. I’m going to call this type of puzzle “overload arrangement.”

Read More

C++ Pub Quiz at CppCon 2021, Game Two

Last night I emceed Game 2 of CppCon 2021’s “C++ Pub Quiz.” This time, five teams participated, and the results were again pretty close: In first place, the ironically named Team Dead Weight with 29.5 points, followed by decltype([](){}) at 28 and the Metaprogrammers at 27. Thanks also to the Leftovers and the Abominable Types.

Read More

C++ Pub Quiz at CppCon 2021, Game One

CppCon 2021 is half over already. Tonight I emceed the first game of the third annual CppCon “C++ Pub Quiz.” Herb Sutter offered opening remarks, and then joined one of our seven teams for a few rounds of play. What is C++ Pub Quiz, you ask? It’s a straightforward trivia night — five rounds, ten questions per round, write down your team’s answers on a sheet of paper and turn it in at the end of the round. High score after five rounds wins the game.

Seven teams participated in this year’s Pub Quiz. The winning team of the night was Team BYOB, with a score of 33 points, just edging out Team Hufflepuff at 32. Third place went to Rotate! That’s A with 30 points. Thanks also to Symbol Not Found; Unit Tested; the Reference ₩rappers a.k.a. UB Unicorns; and Flying Blind.

Read More

operator<=> doesn’t obsolete the hidden friend idiom

C++20 introduces new expression-rewriting rules for comparison operators, such that the compiler can rewrite a < b as either (a <=> b) < 0 or 0 < (b <=> a), and a != b as either !(a == b) or !(b == a). Today, someone asked whether these new rules decrease the attractiveness of the hidden friend idiom for overloaded operators — at least for operator<=>, should we just declare it as a member function? The answer is no. Observe (Godbolt):

Read More

Rolling 2d6 with playing cards

Last night Alexandre Muñiz gave a Celebration of Mind talk titled “How to Roll Two Dice.” If you want to roll 2d6, you can do it either the boring way (by rolling two ordinary d6s) or the interesting way — by rolling two distinct d6s, one labeled 1-2-2-3-3-4 and the other labeled 1-3-4-5-6-8. These Sicherman dice (invented by George Sicherman and first reported by Martin Gardner in 1978) produce the same distribution of 36 possible results as the ordinary 2d6:

Read More

Back to Basics at CppCon 2021

CppCon 2021 starts in just a few days! As in 2019 and 2020, CppCon 2021 will have a “Back to Basics” track. The Back to Basics track’s mission (in phrasing newly refined for 2021) is: Top trainers clearly explain all the essential topics of modern C++.

Read More

Evasive Maneuvers (1994)

I was recently thinking about one of the DOS shareware games of my youth: Evasive Maneuvers (Colin Buckley and Chris Blackwell, 1994). It’s a really polished and addictive VGA side-scroller in which you pilot a little Imperial-Shuttle-ish spacecraft through obstacle-strewn levels, trying to keep up with the implacable scrolling of the screen (á là Flappy Bird (2013)) while shooting and dropping bombs to eliminate hazards and to keep your ship fueled by exploding rooftop fuel tanks. It’s a clone/homage to Scramble (Konami, 1981), which I’ve personally never played but which has clearly spawned a lot of imitators.

Read More