ADT
aaron-thesis
arm-eh
ast-experimental
cleanup-dtors
deferred_resn
demangler
enum
forall-pointer-decay
jacob/cs343-translation
jenkins-sandbox
new-ast
new-ast-unique-expr
new-env
no_list
persistent-indexer
pthread-emulation
qualifiedEnum
resolv-new
with_gc
Last change
on this file since 9bd6105 was 240e1ff, checked in by Rob Schluntz <rschlunt@…>, 8 years ago |
added a simple test for generic unions
|
-
Property mode
set to
100644
|
File size:
492 bytes
|
Rev | Line | |
---|
[240e1ff] | 1 | #include <limits>
|
---|
| 2 |
|
---|
| 3 | forall(otype T)
|
---|
| 4 | union ByteView {
|
---|
| 5 | T val;
|
---|
| 6 | char bytes[(sizeof(int))]; // want to change to sizeof(T)
|
---|
| 7 | };
|
---|
| 8 |
|
---|
| 9 | forall(otype T)
|
---|
| 10 | void print(ByteView(T) x) {
|
---|
| 11 | for (int i = 0; i < sizeof(int); i++) { // want to change to sizeof(T)
|
---|
| 12 | printf("%02x", x.bytes[i] & 0xff);
|
---|
| 13 | }
|
---|
| 14 | }
|
---|
| 15 |
|
---|
| 16 | forall(otype T)
|
---|
| 17 | void f(ByteView(T) x, T val) {
|
---|
| 18 | print(x);
|
---|
| 19 | printf(" ");
|
---|
| 20 | x.val = val;
|
---|
| 21 | print(x);
|
---|
| 22 | printf("\n");
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | int main() {
|
---|
| 26 | ByteView(unsigned) u = { 0 };
|
---|
| 27 | ByteView(int) i = { 0 };
|
---|
| 28 | f(u, MAX);
|
---|
| 29 | f(i, -1);
|
---|
| 30 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.