Skip to main content


Showing posts from April, 2010

Generic Transpose using boost tuples

Recently, while working on LEESA I faced an interesting problem of creating a transpose using generic programming. The problem is pretty straight forward. Suppose you are given a tuple of 2 vectors i.e. tuple<vector<int>, vector<long> >. Assuming both the vectors are of the same size, how would create a vector <tuple<int, long> > containing the data from the earlier two vectors? Of course, the size of the tuple may vary. Lets call the function transpose. A brute force solution would be to overload transpose function N times such that each one takes different size of tuple. Here's one that takes a three parameter tuple. template <class C1, class C2, class C3> // Assuming all Cs are STL containers std::vector<tuple<C1::value_type, C2::value_type, C3::value_type> > transpose (tuple<C1, C2, C3> const &) { ... } } The implementation is fairly straight forward for those who have seen tuples in the past. We basically need a loop wi