Boost logo

Boost Users :

Subject: Re: [Boost-users] [Boost.Range] combine, native range-for, and destructuring
From: Michel Morin (mimomorin_at_[hidden])
Date: 2018-04-17 15:00:03


John M. Dlugosz via Boost-users wrote:
> for (auto&[def,residue] : boost::combine(playdef, residues)) {
>
> gives me an error

This fails, because the deref operator of zip_iterator generates
a prvalue (of a tuple of references). Instead, you can use
- for (auto [x, y] : combine(...)) {...}
- for (auto const& [x, y] : combine(...)) {...}
- for (auto&& [x, y] : combine(...)) {...}

> for (auto row : boost::combine(playdef, residues)) {
> auto&[def, residue] = row; // line 1
> row.get<1>() = 17; // line 2
> residue= 42; // line 3
>
> Now line2 does work, and I note that assignment goes through and updates the
> original
> `residues` vector.
>
> The destructuring on line 1 does compile now, being on a separate line.
> But line 3 gives me an error about not being able to assign to a crazy
> complicated type.

Hmm..., with boost (develop branch) + GCC 7.3 or Clang trunk,
the following code runs fine:

    std::vector<int> v1{1, 1, 1};
    std::vector<int> v2{2, 2, 2};

    for (auto&& tpl : boost::range::combine(v1, v2)) {
        auto [x, y] = tpl; // `auto&`, `auto const&` and `auto&&` also work fine
        x = 10;
        y = 20;
    }

    for (auto&& [x, y] : boost::range::combine(v1, v2)) {
        x = 200;
        y = 200;
    }

Regards,
Michel


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net