It's been at least a decade since I last touched C++. Supposedly, C++ has had a minor overhaul. Is there a way to specify the bit width of a type without using struct, class, or union bitfields? The problem with them is that it adds an unnecessary and annoying level of indirection:
struct Address
{
unsigned char val:4 = 0; // C++ in 2020?
};
struct Device
{
Address address; // 4-bit address
string name;
};
int main()
{
Device device;
device.address.val = 0x8; // Yuckity yuck WTF!
return 0;
};
If C++ had properties like C#, you could make Address an accessor that hides away the indirection. In Ada, you would simply declare Address like so:
type Address is range 0..2**4 - 1 with Object_Size = 4; -- Isn't this cute and sweet!
I tried the following declaration and there's no reason why it shouldn't work:
typedef unsigned char Address:4; // if we were only so lucky!
Does C++ support such a construct or workaround?