[cfe-dev] Structured bindings seem to allow non-const references to a bitfield
James Dennett via cfe-dev
cfe-dev at lists.llvm.org
Wed Mar 22 12:35:27 PDT 2017
On Wed, Mar 22, 2017 at 12:01 PM, Keane, Erich via cfe-dev <
cfe-dev at lists.llvm.org> wrote:
> I wasn't sure whether to bring this up here, or to bring it up on the
> reflector, but I'm not sure if this is a compiler issue or a standards
> issue. I read through the structured binding wording, and don't see
> anything one way or the other, and GCC7 and Clang trunk both allow the
> following:
>
> struct F {
> unsigned a : 1;
> unsigned b : 2;
> unsigned c : 8;
> };
>
> int main() {
> F f;
> f.a = 0;
> f.b = 1;
> f.c = 4;
>
> auto &b = f.b; // correctly errors on non-const ref to a bitfield
>
> auto& [d,e,g] = f; // all are references to a bitfield
>
The only reference here is an unnamed reference to `f`. None of `d`, `e`,
`g` is a reference. `d` is another name for `f.a`, and similarly for `e`
and `g`.
> d = 3; // Works, but warns about truncation
> c = 3; // Works, no diagnostic
> }
>
> Is this intended? Is this justified by the standard?
>
The behavior you describe appears correct (and unsurprising to me), apart
from the assumption that these are references; they're "structured
bindings", not references.
-- James
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20170322/58a044b8/attachment.html>
More information about the cfe-dev
mailing list