Beispiellösungen

Content

Erste Aufgabe

#include <iostream>
#include <utility>

void f() {
}

template<typename Arg, typename... Args>
void f(Arg arg, Args... args) {
   std::cout << arg << std::endl;
   f(args...);
}

int main() {
   f("hi", 3.14, 42);
}
theon$ g++ -Wall -o printit printit.cpp
theon$ ./printit
hi
3.14
42
theon$ 

Zweite Aufgabe

#ifndef UPDATE_HPP
#define UPDATE_HPP

#include <utility>

template<typename F, typename Variable>
void update(F&& f, Variable& var) {
   f(var);
}

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

#endif
#include <iostream>
#include "update.hpp"

int main() {
   std::string s; double x; int i;
   update([](auto& var) { std::cin >> var; }, s, x, i);
   std::cout << "s = " << s << std::endl;
   std::cout << "x = " << x << std::endl;
   std::cout << "i = " << i << std::endl;
}
theon$ g++ -Wall -o testit testit.cpp
theon$ echo Hi 3.14 42 | ./testit
s = Hi
x = 3.14
i = 42
theon$