[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:01:14 PDT 2017


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
  d = 3; // Works, but warns about truncation
  c = 3; // Works, no diagnostic
}

Is this intended?  Is this justified by the standard?
-Erich



More information about the cfe-dev mailing list