Skip to content

Instantly share code, notes, and snippets.

@seanbaxter
Created October 10, 2019 16:10
Show Gist options
  • Save seanbaxter/449bd9a599b163bdd0c38514ef968ddb to your computer and use it in GitHub Desktop.
Save seanbaxter/449bd9a599b163bdd0c38514ef968ddb to your computer and use it in GitHub Desktop.
Bitfield width specifiers
#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;
}
$ 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