Created
October 10, 2019 16:10
-
-
Save seanbaxter/449bd9a599b163bdd0c38514ef968ddb to your computer and use it in GitHub Desktop.
Bitfield width specifiers
This file contains hidden or 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
#include <cstdio> | |
int read_value(const char* prompt) { | |
puts(prompt); | |
int x = 0; | |
scanf("%d", &x); | |
return x; | |
} | |
struct S { | |
constexpr S(int value) : value_(value) { } | |
constexpr operator int() const { | |
return value_; | |
} | |
int value_; | |
}; | |
template<typename type_t> | |
struct X { | |
int value1 : S { 13 } { 17 }; | |
int value2 : (@meta read_value("enter bitfield size:")) { 18 }; | |
}; | |
int main() { | |
X<double> x { }; | |
printf("value1 = %d value2 = %d\n", x.value1, x.value2); | |
return 0; | |
} |
This file contains hidden or 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
$ circle bitfield.cxx | |
enter bitfield size: | |
11 | |
$ ./bitfield | |
value1 = 17 value2 = 18 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment