Ok suppose I have a struct
like so:
struct Example{
uint8_t var0;
uint32_t var1;
uint16_t var2;
};
And suppose I have an instance (note constexpr) of the class that looks like this:
constexpr Example exp = {10,11,12};
And I want to somehow get the bit representation of this into a template parameter pack.
An example of what this would look like:
typedef SomeTemplate<
/* first variable: 1 byte */ 10,
/* second variable: 4 bytes */ 11, 0, 0, 0,
/* third variable: 2 bytes */ 12, 0> mydef;
My knee jerk response to doing this is to do some template meta programming magic with some unions, but unfortunately this is not possible because accessing a union in the way I want is undefined behavior at compile time (ie error).
My ultimate goal with all of this is to put an instance of a user defined type into as a template parameter....something like this:
template<Example exp>
class OtherClass{
};
So the reason I am on this rabbit trail is I could take class Example and give a templated constructor which would take a list of uint8_t
s in and then initialize itself that way (thus still constexpr
and effectively allowing me to pass it in directly).
So is there any way to convert an arbitrary struct instance into an bit pattern at compile time (and all constexpr
)?
Aucun commentaire:
Enregistrer un commentaire