Listing 4
#include <tuple>
#include <utility>
#include <iostream>
using std::tr1::tuple; using std::tr1::tie; using std::tr1::get;
using std::tr1::ignore; using std::tr1::make_tuple; using std::tr1::ref;
using std::cout;
static tuple<double, double, double> get_location(double base)
{ // trivial function that returns a tuple object
return make_tuple(base, base + 1.0, base + 2.0);
}
static void show(double x, double y, double z)
{ // show three values
cout << '(' << x << ',' << y << ',' << z << ')' << '\n';
}
int main()
{ // demonstrate use of tie and ignore
double x, y, z;
// the hard way:
tuple<double, double, double> tpl = get_location(0.0);
x = get<0>(tpl);
y = get<1>(tpl);
z = get<2>(tpl);
show(x, y, z);
// the not-quite-so-hard way:
make_tuple(ref(x), ref(y), ref(z)) = get_location(3.0);
show(x, y, z);
// the easy way:
tie(x, y, z) = get_location(6.0);
show(x, y, z);
// get z only:
tie(ignore, ignore, z) = get_location(9.0);
show(x, y, z);
return 0;
}