Is there any way in C++ define a type that is big enough to hold at most a specific number, presumably using some clever template code. For example I want to be able to write :-
Integer<10000>::type dataItem;
And have that type resolve to the smallest type that is big enough to hold the specified value?
Background: I need to generate some variable defintions using a script from an external data file. I guess I could make the script look at the values and then use uint8_t
, uint16_t
, uint32_t
, etc. depending on the value, but it seems more elegant to build the size into the generated C++ code.
I can't see any way to make a template that can do this, but knowing C++ templates, I'm sure there is a way. Any ideas?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…