#include <iostream>
#include <map>
#include <string>
#include <tuple>
std::tuple<std::string, int> findUser() { return std::make_tuple("Ann", 31); }
// *** before: ***
void oldWay() {
std::string name;
int age;
std::tie(name, age) = findUser(); // the variables had to exist first
std::cout << name << " " << age << "\n";
std::map<std::string, int> ages{{"Bob", 27}, {"Ann", 31}};
for (std::map<std::string, int>::const_iterator it = ages.begin();
it != ages.end(); ++it)
std::cout << it->first << " " << it->second << "\n";
}
// *** in version C++17: ***
struct Point { int x, y; };
void newWay() {
auto [name, age] = findUser(); // declared and filled at once
std::cout << name << " " << age << "\n";
std::map<std::string, int> ages{{"Bob", 27}, {"Ann", 31}};
for (const auto& [key, value] : ages) // no more first and second
std::cout << key << " " << value << "\n";
Point p{3, 4};
auto [x, y] = p; // a plain struct works too
std::cout << x + y << "\n";
auto& [first, second] = p; // a reference lets you write back
first = 10;
std::cout << p.x << "\n";
}
int main() {
oldWay();
newWay();
}