Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default values to bitfield elements

In C++11 one can do

struct S {int i = 42;};

and if one forgets to initialize the member i it gets default initialized to 42. I Just tried this with bitfields as

struct S {int i = 42 : 5;};

and am getting

error: expected ';' before ':' token

Does this feature exist for bitfield members and if so, how do I use it?

like image 870
Vorac Avatar asked Nov 21 '14 09:11

Vorac


1 Answers

The syntax for bit field initialization is

 struct S {int i: 5 = 42;};

and is only available in c++20: -std=c++2a for now

like image 130
Charles Gueunet Avatar answered Oct 06 '22 23:10

Charles Gueunet