Problem

In one of previous posts (Readable for_each loop for maps) I have written such code which iterates over range of pairs and “explode” them into the lambda arguments list:

This code is good but it is limited only to containers with pairs (types with defined first and second  attributes). What we would like to have more is ie. method  for_each_tuple(const Range& range, Fun f) which can be run on any range with any  tuple objects. In example I would like to call it like this:

assuming that students is container with std::tuple<std::string, std::string, int>  objects.

Solution

In C++11 we can implement this like this (solution based on work of Alexander Alexandrescu)

In C++14 we can make it even simpler

What is more previous example with student points in C++14 can be simplified to:

It is more generic now – do you like it? I like it very much.

Expert question

Can we make it even better?

About 

Service Delivery Manager and Scrum Master at Sii Poland company.

Since 2007 C++ enthusiast. C++11/14 evangelist - learn modern ways of developing in C++. Teaching TDD and agile way of coding.

  • googleplus
  • linkedin
  • twitter

Leave a Reply