This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/84855] structered bindings require "decomposed" type to be copy'able
- From: "redi at gcc dot gnu.org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Wed, 14 Mar 2018 11:13:14 +0000
- Subject: [Bug c++/84855] structered bindings require "decomposed" type to be copy'able
- Auto-submitted: auto-generated
- References: <bug-84855-4@http.gcc.gnu.org/bugzilla/>
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