A compile-time benchmark for enable_if and requires

A lot of templates in the C++ standard library are specified with Constraints: elements, which means that the library vendor must ensure that they drop out of overload resolution when their constraints are not met — but the Standard does not mandate any particular mechanism by which the vendor must implement that dropping-out. Two possible mechanisms are SFINAE using std::enable_if, and adding C++20 constraints using the requires keyword.

Read More

Two old crosswords of mine

My lovely wife and I have just moved house, and while boxing up the contents of my desk I ran across physical paper copies of two weakly themed crosswords I made in 2011. Might as well immortalize them in the digital realm…

Read More

Field-testing P2266 “Simpler Implicit Move”

A few months ago, Matheus Izvekov implemented my WG21 proposal P2266 “Simpler Implicit Move” in Clang. As of Clang 13, it’s enabled by default under -std=c++2b.

We’ve received some feedback from the field already (leading up to the Clang 13 release). Here are the punch lines as far as I can recall. These examples will all appear in P2266R2, whenever I get around to submitting it.

Many thanks to Matheus Izvekov for the implementation, and to Stephan Bergmann for reporting these issues! …

Read More

Buddhabrot

You’ve heard of the Mandelbrot set, right? Basically, for each point \(c\) on the complex plane, we iterate the function \(z = z^2+c\) and color the point \(c\) based on how many steps it takes until \(\lvert z\rvert\geq 2\) (which is known as “diverging” or “escaping”).

I recently learned of another visualization called the “Buddhabrot,” originally discovered by Melinda Green. In the Buddhabrot, you color each complex point \(t\) based on the number of times that \(t\) is visited by the escaping trajectories of all points \(c\). As Wikipedia says, this produces a graph of “the probability distribution over the trajectories of points that escape.”

Read More

Implementation divergence on swapping bools within vectors

Today, while inventing questions for my next C++ Pub Quiz, I ran into a fairly obscure and meaningless divergence among vendors’ implementations of vector<bool>::reference. Given:

std::vector<bool> v = {true, true};

The correct way to swap v[0] with v[1] is of course swap(v[0], v[1]), optionally preceded by using std::swap; see “What is the std::swap two-step?” (2020-07-11). But look at all these wrong things you can try! …

Read More

Another requires-clause syntax pitfall

In my CppCon 2018 talk “Concepts As She Is Spoke,” I presented some pitfalls in requires-clause syntax, such as:

template<class T>
concept HasHash = requires (const T& t) {
    t.hash() -> std::integral;
};

struct X { int hash() const; };
static_assert(!HasHash<X>);

On the cpplang Slack today, John Eivind Helset and Eugenio Bargiacchi (inadvertently) showed me a new trap I hadn’t seen before: …

Read More

Tag dispatch versus concept overloading

In Bjarne Stroustrup’s paper P0557R1 “Concepts: The Future of Generic Programming” (January 2017), section 6 is titled “Concept Overloading,” which Stroustrup distinguishes from the traditional approach “using helper functions and tag dispatch.” More subtly, Stroustrup seems to distinguish between true “overloading” (asking the compiler to resolve among multiple viable overloads) and other means of giving-two-things-the-same-name:

Where we cannot overload, we need to use workarounds (e.g., traits, enable_if, or helper functions).

I’ll present two problems, each with two solutions (one in C++11 and one in C++20), which I think illustrate something interesting about “concept overloading” versus… other things. …

Read More

A Night of Serious Drinking and operationalism

A few weeks ago, I read René Daumal’s A Night of Serious Drinking (published as La grande beuverie 1938; translated by David Coward and E.A. Lovatt 1979). I recommend it; it’s a fun and erudite satire — sort of a less political Gulliver’s Travels or a less bitter Jurgen. At times it made me think, “This is clearly an allegory… but of what?” and at other times “Is he satirizing bad scientists, bad writers, bad artists? or all scientists, all writers, all artists?”

Read More

C++ On Sea 2021 schedule announced

The schedule for C++ on Sea 2021 has been announced! The “conference day” involves two tracks of five talks each, plus a keynote by Barbara Geller and Ansel Sermersheim. The two “workshop days” offer… well, you should sign up for my own two-day training class, at which point the other offerings are moot. But, just for the record, the two available two-day classes are:

If you don’t do a two-day class, then on Wednesday you can choose among these options: …

Read More

Overly interoperable libraries, part 3

Previously on this blog:

Jason Cobb has sent me a proposal that I think meets all the requirements and therefore qualifies as a complete solution!

Unlike the ideas in my previous posts, Jason’s solution doesn’t depend on “GUIDs” at all. It’s based on a simple idea that is, in hindsight, quite obvious. …

Read More