Tag Archives for " tuple "

How to Define Comparison Operators by Default in C++

Published August 23, 2021 - 0 Comments

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 […]

How to Return Several Values from a Function in C++

Published July 9, 2021 - 0 Comments

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 […]

std::index_sequence and its Improvement in C++20

Published March 5, 2021 - 0 Comments

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 […]

The differences between tie, make_tuple, forward_as_tuple: How to Build a Tuple in C++?

Published October 16, 2020 - 0 Comments

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 […]

STL Algorithms on Tuples

Published March 8, 2019 - 0 Comments

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 […]