No ... you cannot do this as a single semantic value type ... any class data must be a multiple of the 8-bit character size (inviting all the usual hints about CHAR_BITS, etc.).
However, let it lay on a straw ...
Unfortunately, you are obviously processing a lot of data items. If it's larger than 64k, any proxy object in a custom container with packed values ββwill probably also need a> 16-bit index / descriptor, but still one of the few features that I can see deserves further consideration. This can be convenient if you are only actively working and need a semantic value value for a small subset of values ββat one point in time.
struct Proxy { Int20_Container& container_;
Thus, the proxy can be 32, 64 or more bits - the potential advantage is only that you can create them on the fly from the indices in the container, ask them to write back back to the container and keep them short-lived with several at the same time. (One simple way β not necessarily the fastest β to implement this model is to use the STL bitmap or vector as an Int20_Container and either store the logical index 20 times in index_ or multiply by the fly.)
It is also vaguely possible that although your values ββvary in 20-bit space, in actual use you have less than 64k different values. If you have such an idea about your dataset, you can create a lookup table where the indexes of 16-bit arrays correspond to 20-bit values.
source share