I am looking for a construct that allows the compiler to select the minimum data type for a given integer in a template parameter. The problem why I could not find a solution on my own is how I want to use it:

```
template<typename T, min_type<max_elements_to_store> max_elements_to_store>
class testClass {
private:
    T data[max_elements_to_store];
    min_type<max_elements_to_store> currently_selected_element;
};

```

The macro "min\_type" should dynamically choose the type with minimum number of bits for the given max\_elements\_to\_store (uint8\_t, uint16\_t, uint32\_t, uint64\_t). I could fix it by simply replacing the min\_type<> with a given data type but this data type would often not be the best possible selection. E.g.,

```
template<typename T, uint64_t max_elements_to_store>
class testClass {
private:
    T data[max_elements_to_store];
    uint64_t currently_selected_element;
};

TestClass<uint8_t, 12> testObject;

```

Here, the array would only hold 12 elements and the currently\_selected\_element variable wastes many bits that are simply not necessary for accessing only 12 elements. This seems to be only a small problem but it gets worse for many variables for accessing the data in the class...

Is there a solution for this problem? I hope it is clear what I'm looking for. 

Thanks in advance!
Inspire