It seems JavaScript and Ruby like destructuring has come to C++17 as
Structured Bindings. This provides shorthand notation to pull out elements of arrays, tuples, and structs, into new variable declarations.
Examples:
#include <iostream>
#include <tuple>
The case of the bitfield reference is particularly interesting. When working with GCC, I see warnings if I try to set the reference value beyond what the bitfield can hold. That suggests it's able to keep track of the restricted range of the bitfield.
S s1{0, 16};
auto& [s1f1, s1f2] = s1;
s1f1 = 16;
If I had instead tried to set a normal reference, it would have failed:
The closest I've been able to do, is bind to a temporary copy of the value stored in the bitfield. This converts to the underlying type, and so the bitfield restriction is lost. If the binding is an rvalue reference, you can modify the temporary. If you use an lvalue reference, it must be declared const for the binding to be allowed. In either case, the binding is to a temporary copy, and so changes to the original field are not reflected: