Data race when catching by non-const reference

People say “always catch exceptions by const reference,” which I think is good advice. I’ve worked on at least one codebase that catches by non-const reference, presumably just because it’s shorter to type. This has never caused any problem for them in practice, as far as I know. But in theory, it’s a bad idea, because in C++ it is possible to construct a program where two different catch-blocks, executing concurrently, actually reference the exact same exception object. In that situation, any mutation to the exception object will cause a data race. The easiest way to prevent the race is to prevent mutation: to catch by const reference!

Read More

The big problems for C++11 SFINAE

So I’ve finally started working on slides for my CppCon 2018 talk “Concepts As She Is Spoke.” (To see the original work to whose title this is an allusion, click through.) I intended to make it as bland and tutorial in nature as I can, but my natural tendency to find the hard problems keeps interfering. Let me take the 25 slides I just wrote and turn them into a blog post, so that I can qualmlessly delete these slides and start over.

Read More

make_range and reversed

A simple force multiplier that should be in every C++11-and-later programmer’s toolbox:

template<class It>
struct iterator_range {
    It begin_, end_;
    constexpr It begin() const noexcept { return begin_; }
    constexpr It end() const noexcept { return end_; }
}; ...
Read More