I have a struct which contains bit-fields:
struct Foo {
unsigned a : 16, b : 16;
};
And I want to know if I can use aggregate initialization on it's bit-fields. For example:
struct Foo bar = {13, 42};
I note that this does work in gcc 5.1 and Visual Studio 2015. I'd just like something to certify this was standard approved initialization for both C and C++.
From C++14
[dcl.init.aggr]
we haveSo
Foo
is an aggregate an qualifies for aggregate initialization. Then we haveand
So in your case they will be initialized since they are not anonymous and they will be initialized in the order they appear in the
struct
.From C11 6.2.5(21) we have
So in C we are still dealing with an aggregate. Then in 6.7.9(9) we have
and 6.7.9(17)
So we have the same behavior as in C++ where anonymous bit fields are not initialized but since they are named they will be initialized in the order they appear in the
struct
.