Last active
August 29, 2015 14:02
-
-
Save kayru/327ad61b698f09642ab6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void fun() | |
{ | |
union Test1 | |
{ | |
struct | |
{ | |
u32 foo : 2; | |
u32 bar : 2; | |
}; | |
u32 packed; | |
}; | |
Test1 a, b; | |
a.foo = 0; a.bar = 1; | |
b.foo = 2; b.bar = 3; | |
a.bar = b.bar; // only 'bar' bits are copied | |
assert(a.foo == 0); // works as expected | |
union Test2 | |
{ | |
BitField<0, 2, u32> foo; | |
BitField<2, 2, u32> bar; | |
u32 packed; | |
}; | |
Test2 c, d; | |
c.foo = 0; c.bar = 1; | |
d.foo = 2; d.bar = 3; | |
c.bar = d.bar; // entire 'c' is overwritten by 'd' because bitfield types match and custom operator is not called | |
assert(c.foo == 0); // oops | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment