This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


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

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]