Beispiellösung

Content

#ifndef UPDATE_HPP
#define UPDATE_HPP

#include <cstdlib>
#include <utility>

namespace update_with_index_impl {

   template<typename F, typename Variable>
   void update(F&& f, std::size_t size, std::size_t index, Variable& var) {
      f(size, index, var);
   }

   template<typename F, typename Variable, typename... Variables>
   void update(F&& f, std::size_t size, std::size_t index,
	 Variable& var, Variables&... vars) {
      f(size, index, var); update(std::move(f), size, index+1, vars...);
   }

} // namespace update_with_index_impl

template<typename F, typename... Variables>
void update_with_index(F&& f, Variables&... vars) {
   update_with_index_impl::update(std::move(f),
      sizeof...(vars), 0, vars...);
}

#endif
#include <iostream>
#include "update-with-index.hpp"

int main() {
   std::string s; double x; int i;
   update_with_index([](std::size_t size, std::size_t index, auto& var) {
      std::cout << "Variable " << index+1 << "/" << size << ": ";
      std::cin >> var;
      std::cout << "Got " << var << std::endl;
   }, s, x, i);
}
theon$ g++ -Wall -o testit-with-index testit-with-index.cpp
theon$ echo Ulm 3.14 42 | ./testit-with-index
Variable 1/3: Got Ulm
Variable 2/3: Got 3.14
Variable 3/3: Got 42
theon$