DEV Community

Discussion on: Variadic Template C++: Implementing Unsophisticated Tuple

Collapse
 
siy profile image
Sergiy Yevtushenko • Edited

I have an impression that there is no need to loop through tuple elements nor access tuple elements via getters of any kind. Instead tuple should have method (for example, 'map') which accepts lambda with the number and order of arguments equal to number/order of elements in tuple. Method returns whatever returns passed lambda. This method makes something like 'destructuring' of tuple into set of locally named elements - lambda parameters. At first this approach looks somewhat strange, but in practice it appears to be far more convenient and much less error prone than getters.
With this approach it's convenient to think about tuples as a set of "frozen" parameters prepared for function invocation. The invocation itself is deferred (and often unknown at the moment when tuple is created). The 'map' method then is the way to perform that deferred invocation.

Disclaimer: I've used this approach in Java with my own implementation of tuples. My implementation is quite "C++ - like" (i.e. types of all tuple elements are preserved as class type parameters), so approach should be applicable to C++.

EDIT: The approach is described in more details here.