Implementing comparison operators in C++ is easier said than done. Indeed, for most types, if we could talk to the compiler we would say something like: “to order them, use a lexicographical order on their members”. But when it comes to writing the corresponding code, things get more complicated. However, a classical technique using std::tuple makes […]
Functions should take their inputs as parameters and produce outputs with their return types. This is the basics of functions interface design. This makes functions easier to understand just by looking at their prototype. It makes functions functional. But C++ only allows to return one value out of a function. What if we’d like to […]
It would be great if we could iterate on the values of a std::tuple like we do for a std::vector or for other STL containers. But we can’t. To iterate on the values inside of a tuple, we need to proceed in two steps: instantiate a std::index_sequence object with std::make_index_sequence, pass it to another function that performs the […]
Tuples are handy C++ components that appeared in C++11, and are a very useful help when programming with variadic templates. To make things even simpler, C++ offers not one but three helpers to build tuples and make our variadic template code more expressive: std::make_tuple, std::tie and std::forward_as_tuple. All three reflect in their name the fact that they put […]
When you manipulate a collection of objects in C++–which is quite a common thing to do when programming in C++–STL algorithms are your loyal companions to perform operations with expressive code. But the STL algorithms, shipped in the standard library with C++, only apply to collections that are filled at runtime, during the execution of […]
Smart output iterators are output iterators that do more than just sending a piece of data from an STL algorithm to a container. They can embed logic that relieves the algorithm of some of its responsibilities. We have already seen examples of smart output iterators that apply a function or filter on a predicate. Now let’s […]
How to store a variable number of objects known at compile time? This is a question that Fluent C++ reader John Koniarik sent in to me by email. Here was his exact problem, reproduced with his permission: I would like to efficiently store n-dimensional simplexes. I have defined Simplex< unsigned n > as key data […]