What's the right hash table API?
Barry Revzin
by Barry Revzin
11M ago
The Associative Container API When it comes to a hash table, there are basically only two interesting operations from an API perspective: lookup and insertion 1. The standard library containers are all very consistent in how these operations are provided, across all the associative containers (maps and sets both, but I’ll just focus on maps): template <class Key, class Value /* and other p ..read more
Visit website
Mutating through a filter
Barry Revzin
by Barry Revzin
1y ago
Introduction Nico Josuttis gave a talk recently that included an example like this: std::vector<int> coll{1, 4, 7, 10}; auto isEven = [](int i) { return i % 2 == 0; }; // increment even elements: for (int& i : coll | std::views::filter(isEven)) { ++i; // UB: but works } I wanted to explain what’s going on in this example, what the issue is, and what (if anything) is broken ..read more
Visit website
What's so hard about views::enumerate?
Barry Revzin
by
1y ago
Sometimes, we want more than to just iterate over all the elements of a range. We also want the index of each element. If we had something like a vector<T>, then a simple for loop suffices 1: for (int i = 0; i < vec.size(); ++i) { // use i or vec[i] } But for a range that can’t be indexed like this (or a range for which indexing like this means something else entirely, like map<int, T>), we need to do it differently. You could write: int index = 0; for (auto&& elem : r) { // use index or elem ++index; } But that’s a bit fragile, since if you added a conti ..read more
Visit website
Copy-on-write with Deducing this
Barry Revzin
by
1y ago
One of the new language features for C++23 is Deducing this, which is a feature I co-authored with Gašper Ažman, Sy Brand, and Ben Deane. The interesting history there is that Sy and I were working on solving one problem (deduplication of all the const and ref-qualifier overloads) and Gašper and Ben were working on another (forwarding lambdas) and it just so happened that Jonathan Wakely pointed out that we were converging on a similar solution. In short, the facility allows you to declare an explicit object parameter (whereas C++ has always let you have an implicit object parameter, that this ..read more
Visit website
Assignment for optional<T>
Barry Revzin
by
2y ago
Let’s talk about assignment for optional<T>. I realize this is a fraught topic, but I want to try to build up proper intuition about how assignment has to work, especially since the debate around this topic has been fairly underwhelming. This post will almost exclusively discuss copy assignment (i.e. the one that takes an optional<T> const&), since everything just follows from that. As a quick intro, I am going to use the following layout: template <typename T> class Optional { union { T value_; }; bool has_value_; }; In C++17, you’d see an additiona ..read more
Visit website
Projections are Function Adaptors
Barry Revzin
by
2y ago
There was a question recently on StackOverflow where a user was confused about the purpose of projections in a way that I think is fairly common. They wanted to do something like this: struct Person { std::string first; std::string last; }; std::vector<Person> people = { /* ... */ }; std::vector<std::string> r_names; std::ranges::copy_if( people, std::back_inserter(r_names), [](std::string const& s) { return s[0] == 'R'; }, &Person::last); Obligatory link to post about std::back_inserter, or output iterators more generally. The goa ..read more
Visit website
Improving Output Iterators
Barry Revzin
by
2y ago
Let’s say we had a range, represented by a pair of pointers, that we wanted to copy into another pointer. We might write that like so: template <typename T, typename U> void copy(T* first, T* last, U* out) { for (; first != last; ++first) { *out++ = *first; } } For trivially copyable types, this could be improved to memcpy, and maybe you might want to partially unroll this loop since we know how many iterations we have to do – but let’s ignore those kinds of details. We can generalize this to an arbitrary input range and an arbitrary output iterator by simply changing t ..read more
Visit website
T* makes for a poor optional<T&>
Barry Revzin
by
2y ago
Whenever the idea of an optional reference comes up, inevitably somebody will bring up the point that we don’t need to support optional<T&> because we already have in the language a perfectly good optional reference: T*. And these two types are superficially quite similar, which makes the argument facially reasonable. Indeed, optional<T&> would certainly be implemented to have this shape: template <> struct optional<T&> { T* storage = nullptr; explicit constexpr operator bool() const { return storage; } constexpr auto operator*() const -> T ..read more
Visit website
Conditional Members
Barry Revzin
by
2y ago
I’d previously written a post about if constexpr (and how it’s not broken). I argued in that post how, broadly speaking, C++20 gives you the tools to solve the problems you want, even if they work a bit differently to D’s static if (with one notable exception, which this post greatly expands on). Now, over the past couple years, I’ve been working on a project that really is a deep dive into what Andrei calls “Design by Introspection.” This approach (for lack of a better definition), relies on conditioning functionality based on template parameters. For the purposes of this post, I’m going to d ..read more
Visit website
Coercing deep const-ness
Barry Revzin
by
2y ago
In C++, template deduction doesn’t allow for any conversion. A type matches the pattern, or it’s a deduction failure. But there’s one sort-of exception to this rule, and it’s an exception that everyone has taken advantage of: template <typename T> void takes_ptr(T const*); void f(int i) { takes_ptr(&i); } &i here has type int*. There is no T for which that is some T const*, but this compiles and works anyway. An even more common and familiar idiom would be the reference version of this: template <typename T> void takes_ref(T const&); Even if you pass a mutable ..read more
Visit website

Follow Barry Revzin on FeedSpot

Continue with Google
Continue with Apple
OR