Compiletime check for bit-positions in bit-field

Oleg Endo oleg.endo@t-online.de
Wed Dec 4 14:50:00 GMT 2019


On Wed, 2019-12-04 at 15:41 +0100, Klaus Doldinger wrote:
> 
> Am 04.12.19 um 15:33 schrieb Oleg Endo:
> > On Wed, 2019-12-04 at 14:50 +0100, Klaus Doldinger wrote:
> > > Hello,
> > > 
> > > does anybody see a possibility to perform a compiletime-check to
> > > verify
> > > that a bit of a bit-field is at an exact position of the
> > > underlying type?
> > > 
> > > I would see a possible using the forecoming std::bit_cast, but
> > > what can
> > > one do with c++17?
> > > 
> > 
> > Perhaps have a look at this approach
> > https://blog.codef00.com/2014/12/06/portable-bitfields-using-c11/
> 
> Well, I don't think this is correct, since reading/writing from a
> non-active member of a union leads to UB.
> So, I think the blog-post ist simply wrong.

Right, it's UB ... although "most compilers implement it as non-
standard extension".  So I wouldn't say the blog post is wrong per se,
it's just failing to mention a tiny detail :)

Cheers,
Oleg



More information about the Gcc-help mailing list