Skip to content

Instantly share code, notes, and snippets.

@dhollman
Last active November 9, 2021 18:35
Show Gist options
  • Save dhollman/d6f85ea1b6417cf3670d333f845ba731 to your computer and use it in GitHub Desktop.
Save dhollman/d6f85ea1b6417cf3670d333f845ba731 to your computer and use it in GitHub Desktop.
Cute C++ Challenge of the Week

Cute C++ Challenge of the Week

So I've had a lot of fun with my "Cute C++ trick of the day" posts and the talk I gave at CppCon as a result. I wanted to inspire others to create cute tricks and "play" with the language the way I've been over the past few years, so I've decided to start occasionally trying to post the "challenge" I've set for myself when creating a particular "Cute C++ trick of the day" a few days before I post my particular solution.

More details below, but in "TL;DR" form, here's how this works:

  • I'll tweet out a challenge that I have an interesting solution in mind for a few days in advance of posting the corresponding Cute C++ trick of the day.
  • I'll schedule my tweet with the solution at the same time I post the challenge, and I won't change it based on other's solutions.
  • I'll post a unique hashtag for the particular challenge (e.g., #CuteCppNamedTuple) and anyone who wants to participate can tag their solutions so that I'll see them (and so that everyone else will).
  • I'll retweet or quote tweet my favorite solutions and we can all have a good laugh about how bizarre and terrifying C++ is sometimes.

The goals and "rules" (well really there are no rules, but still) of these challenges are too complicated to list out in tweet format, so I wrote this Gist to summarize what I'm after.1

"Code minigolf"

If you've ever heard of "code golf" where you try and use obscure features of a language to solve a programming problem in the fewest characters possible...well this is not that. Matt Kulukundis (@JuvHarlequinKFM on Twitter) suggested the name "code minigolf" for my "Cute C++ tricks" challenges (because you're trying to "go off the windmill, around the corner, through the loop-the-loop, and into the hole," but not necessarily in the most serious, strictly competitive way possible). In no particular order, the goals are:

  • Be surprising: The more someone can learn from your tweet, the better.
    • Packing too many different tricks into one example, though, risks muddying the waters and making your solution less valuable as a learning tool.
  • Be pithy: Good responses should be terse enough to feel pithy and catch someone's attention on Twitter. Shorter is often better for these purposes, but you don't get any bonus points for being 3 characters shorter than someone else's solution.
    • There are no standard rules for formatting, but if it's distracting and doesn't look like a way that anyone would ever format "real" code, you should probably search for other approaches to brevity.
  • Be as readable as possible within space constraints: An answer that can use a few more characters (or even lines!) to be slightly more readable without losing "pithiness" (i.e., it should still feel like a tweet) is better than one that doesn't take these things into consideration.
  • Try to hit the "bonuses" I'll post a few "bonus points" with each challenge.
    • There are always bonus points available for discovering new, previously unknown compiler bugs or (especially) for discovering new, previously unknown issues with the C++ standard itself.
  • Try to use mostly standard C++: Occasional use of third-party libraries is allowed, but try to keep it to a minumum—in my experience this usually detracts from the overall "cuteness". Compiler-specific extensions and intrinsics are also allowed, but try to keep that to a minimum; people are much less likely to find this sort of thing "cute".
  • There are no absolute rules, and no absolute winners: This is for fun, y'all. Feel free to have different opinions about what's "better" or "worse".

Logistically, try to include a screenshot (with alt text!) and a Godbolt link with your solution. Be sure to think about people reading your tweet on their phone (godbolt.org doesn't display well on most phones).

Have fun, play nice, and I look forward to seeing all of your interesting tweets!

Daisy 🌼

Footnotes

  1. I'll probably make this into a part of my personal website at some point somewhere, but I learned this weekend that I'm really bad with jekyll and GitHub pages, so I'm just doing this for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment