February 22, 2015
This is the last post in the series. We have a fully functional tuple class, with a decent implementation of tuple_cat -- but some reservations about its performance. In this post we're going to improve it considerably, at the expense of making the implementation more difficult. The main problem with what we did in the previous post is that we are creating many temporary tuple objects. When concatenating n tuples, we create n-2 intermediate tuples that hold partial results. But can it be avoided?
UPDATE (Feb 24): I forgot to credit Stephan T. Lavavej (a.k.a. STL, also the maintainer of Microsoft's STL)...
February 13, 2015
We are almost done. We have a pretty functional tuple that supports almost every operation mandated by the Standard. With one major exception: tuple_cat. First, let's see what it does:
auto t1 = tuple_cat(make_tuple(1), make_tuple(2)); // tuple<int, int>(1, 2)
auto t2 = tuple_cat(t1, make_tuple(3), make_tuple(4));
auto t3 = tuple_cat(t1, t1, t2, t2);
// t3 is a tuple of 12 ints: 1, 2, 1, 2, 1, 2, 3, 4, 1, 2, 3, 4
Hmpf. Obviously, tuple_cat is a variadic function template. tuple is itself a variadic class template, and we're now trying to build a variadic function template that takes any number of variadic class...
February 3, 2015
After doing all the heavy lifting in the previous four installments (, , , ), this one is going to be very lightweight. We will implement a few simple tuple non-member functions and helpers.
First, let's implement a very simple helper: tuple_size. It is a metafunction that returns the number of elements in the tuple.
Exercise 11: Implement tuple_size.
struct tuple_size; // undefined base template
template <typename... Types>
struct tuple_size<tuple<Types...>> : std::integral_constant<size_t, sizeof...(Types)>
Well, that was easy. Let's do another: forward_as_tuple. The basic idea is that you provide a set of elements and get back a tuple that has lvalue or rvalue references depending...