Why do bitfields throw exceptions instead of wrapping?

ixid nuaccount at gmail.com
Wed Feb 29 19:15:04 PST 2012


In C++ this works:

struct test
{
	unsigned int h : 2;
};

int main()
{
	test b;
	b.h = 0;
	for(int i = 0;i < 10;i++)
		++b.h;
	return 0;
}

In D this throws an exception as soon as it wraps:

struct test
{
	mixin(bitfields!(
					 uint, "h", 2,
					 uint, "", 30));
};

int main()
{
	test b;
	b.h = 0;
	for(int i = 0;i < 10;i++)
		b.h = b.h + 1;
	return 0;
}

Is there any way to make it work directly like the C++ example 
without doing something like
if(bitfield is at max)
     wrap it by hand;

It also seems odd to leave out the various shortcut operators 
like ++, *= etc fr bitfields.


More information about the Digitalmars-d-learn mailing list