[Bug c++/84855] structered bindings require "decomposed" type to be copy'able

redi at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Wed Mar 14 11:13:00 GMT 2018


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84855

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Because 'a' and 'b' are not local variables, they refer directly to subobjects
of the tuple-like object, not separate variables. So if you didn't make a copy
of the foobar then 'a' would refer to f.s and b would refer to f.v and you
would get very surprising results if you modify them:

#include <vector>
#include <string>
#include <cassert>

struct foobar
{
  std::string s{"original"};
  std::vector<int> v;
};

int main()
{
    foobar f;
    assert(f.s == "original");
    auto [ s, v ] = f;
    s = "changed";
    assert(f.s == "original");  //fails if &s == &f.s
}

You have to make a copy of f, and then &s == &__copy_of_f.s


More information about the Gcc-bugs mailing list