Home
Barry's C++ Blog
Cancel

Code Generation in Rust vs C++26

One of the things I like to do is compare how different languages solve the same problem — especially when they end up having very different approaches. It’s always educational. In this case, a bunch of us have been working hard on trying to get reflection — a really transformative language featu...

What's so hard about class types as non-type template parameters?

Setting the Stage Previously, I tried to answer the question: what’s so hard about constexpr allocation?. Today, we continue what will become a series of posts about attempting to explain the issues behind a bunch of hard problems we’re trying to solve. The next problem: class types as non-type ...

What's so hard about constexpr allocation?

Setting the Stage Before C++20, we couldn’t have any allocation during constant evaluation at all. Any attempt to do so would fail the evaluation — it would no longer be constant. In C++20, as a result of P0784R7, that changed. Finally we could do allocation during constant evaluation. However,...

Are we missing a Ranges customization point?

Let’s say we’re writing one container that we’re implementing in terms of another container. If we want to make my container a range based entirely on that other container, that’s easy: template <typename T> class StableVector { std::vector<T*> impl; public: auto begin() con...

What's the right hash table API?

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 (m...

Mutating through a filter

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 want...

Prefer views::meow

With the adoption of Ranges in C++20 (and with a lot of new additions in C++23), we have a lot of new, composable, range-based algorithms at our disposal. There are, however, several ways to spell the usage of such algorithms: std::vector<int> v = {1, 2, 3}; auto square = [](int i){ return...

Getting in trouble with mixed construction

Several years ago, I wrote a post about the complexities of implementing comparison operators for optional<T>: Getting in trouble with mixed comparisons. That post was all about how, even just for ==, making a few seemingly straightforward decisions leads to an ambiguity that different libr...

Rust vs C++ Formatting

In Rust, if I want to print some 32-bit unsigned value in hex, with the leading 0x, padded out with zeros, I would write that as: println!("{:#010x}", value); In C++23, if I want to do the same, that’s: std::println("{:#010x}", value); The only difference is the spelling of the name of the ...

What's so hard about views::enumerate?

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...