[cfe-dev] Structured bindings seem to allow non-const references to a bitfield

Keane, Erich via cfe-dev cfe-dev at lists.llvm.org
Wed Mar 22 12:38:29 PDT 2017


Ok, I guess I didn’t (don’t completely?) ‘get’ that there is a difference between “structured binding alias” and “reference”.  Additionally, if you simply do “auto”, you get copies, right?  So those structured bindings actually introduce new variables, right?

From: James Dennett [mailto:james.dennett at gmail.com]
Sent: Wednesday, March 22, 2017 12:35 PM
To: Keane, Erich <erich.keane at intel.com>
Cc: cfe-dev <cfe-dev at lists.llvm.org>
Subject: Re: [cfe-dev] Structured bindings seem to allow non-const references to a bitfield

On Wed, Mar 22, 2017 at 12:01 PM, Keane, Erich via cfe-dev <cfe-dev at lists.llvm.org<mailto: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/3230a8a6/attachment.html>


More information about the cfe-dev mailing list