while (auto x=y; z)

This question has come up at least twice on the cpplang Slack now. C++ keeps adding more and more knobs to if and for and switch; why hasn’t it messed with while? Specifically, why isn’t there a “two-part while loop”?

Read More

std::once_flag is a glass hill

Yesterday I did a followup Q&A session for my CppCon 2020 presentation “Back to Basics: Concurrency.” (These followup sessions aren’t recorded.) During the Q&A, there was some discussion of std::once_flag. Let’s repeat my table of the three C++11 synchronization primitives (which was briefly flashed as slide 34 in the recorded talk, but I was running behind at that point and didn’t stop to discuss it):

Read More

Conway’s Soldiers

The other day I learned about Conway’s Soldiers: Consider playing peg solitaire on an infinite grid of holes, where initially an infinite number of pegs fill the entire half-plane south of the line \(y=0\). The goal is to advance one peg as far north as possible, by repeatedly jumping and removing pegs in the usual peg-solitaire fashion.

Read More

A case study in implementation inheritance

The more I deal with classically polymorphic code, the more I appreciate the “modern” idioms that have grown up around it — the Non-Virtual Interface Idiom; Liskov substitutability; Scott Meyers’ dictum that classes should be either abstract or final; the rule that every hierarchy should have exactly two levels; and the rule that base classes express commonality of interface, not reuse of implementation.

Today I’d like to present a chunk of code that shows how “implementation inheritance” ended up causing trouble, and the particular patterns I applied to disentangle it. Unfortunately, the offending code is pretty messy, so I’ll use a simplified and domain-shifted example; and I’ll try to build it up in stages. …

Read More

Solo-player rules for Colossal Cave: The Board Game

Back in 2013, I Kickstarted Colossal Cave: The Board Game, a board-game adaptation of Adventure. (See the Kickstarter campaign here. I’m quite proud of that campaign. You can still find copies of the game on Amazon.)

Seven years later, I’ve finally tried out a single-player variant originally inspired by BoardGameGeek poster Tracy Smith, and I’m pleased to say that it works pretty well! In this variant, you’re playing against the clock, trying to deposit as many treasures as you can before the draw pile runs out. …

Read More

Classically polymorphic visit replaces some uses of dynamic_cast

I’ve informally described this C++ utility to several people by now; it’s time I just wrote it up in a blog post. This utility lets you “visit” a polymorphic base object as if it were a variant, by specifying the domain of possible child types at the point of the call to visit. my::visit<X,Y,Z>(b, f) returns f(static_cast<X&>(b)) if the dynamic type of b is exactly X; f(static_cast<Y&>(b)) if the dynamic type of b is exactly Y; and so on. It perfectly forwards the argument when making the call to f.

Read More

September blog roundup

I can’t exactly call this “What I’m reading lately,” because I haven’t gotten around to fully exploring these trees. But here are two blogs I recently discovered with good stuff.

Read More

Why do I get a linker error with static const and value_or?

In my Back to Basics talk on “Algebraic Data Types,” I cut some slides at the last minute. (I still ended up going over time a bit.) Once you’re able to download the slides from CppCon’s slides repository, you’ll see three bonus slides at the end. One was on the fact that you can (but shouldn’t!) use std::variant<T,E> as a poor man’s version of Expected<T>. The other two were on a minor pitfall that seems to bite me every time I update a piece of legacy code to use std::optional.

Read More

C++ Pub Quiz at CppCon 2020, Game Two

The return of Anagram Time Last night I emceed Game 2 of CppCon 2020’s “C++ Pub Quiz.” This time, six teams participated. Thanks to std::locale::facet, Team SFINAE, 2*Alex, auto team;, std::make_exceptional_future, and our winning team, Team SNPIOR (“Shall Not Participate In Overload Resolution”)!

Read More

C++ Pub Quiz at CppCon 2020

Spoiler for question #4 CppCon 2020 is half over; this afternoon I gave my B2B talk on algebraic data types. On Friday I give my B2B talk on concurrency, and next week I’ll deliver my three-day “Classic STL” training course — for which there is still time to register! :)

Tonight I emceed the first game of the second annual CppCon “C++ Pub Quiz.” (There’s a second game happening tomorrow night.) 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 Pub Quiz. Thanks to std::my_teammates_dont_respond_exception, Team C⁠-⁠-, Team Don’t Care, The French Connection, Team Sea Slug, DROP TABLE * FROM ANSWERS, Team 0x1010, and Team IFNDR! …

Read More

“Classic STL” at CppCon 2020

It’s almost time for CppCon 2020! The conference is happening entirely online this year. I’ve been quite impressed with the amount of polish that’s gone into it. If you’re still on the fence about whether CppCon is right for you, I think 2020 would be a great year to try it out: You can “attend” from home, for a vastly reduced conference ticket price of only $300 for the whole five-day conference. (I have been informed that $50 student-price tickets, also, are still available as of this writing.)

Read More