On Saturday, 7 January 2023 at 00:14:35 UTC, Aaron Gray wrote:
> Hi,
> I am looking for some template code to generate a type based upon a constant within value ranges, for specialization of a bitset class.
>
> 0...7 - unsigned char
> 8..15 - unsigned short
> 16...31 - unsigned int
> 32...63 - unsigned long
> 64...127 - unsigned long long
> 128... - unsigned long long [] - array
>
> ```
> #include <cstddef>
>
> template <size_t BITS>
> class bitset {
> public:
> ....
> WORDTYPE data;
> ....
> };
> ```
ChatGTP has given me the following :-
```
#include <type_traits>
template <size_t BITS>
class bitset {
public:
static_assert(BITS > 0, "Number of bits must be positive");
using WORDTYPE = typename std::conditional<BITS <= 8, unsigned char,
typename std::conditional<BITS <= 15, unsigned short,
typename std::conditional<BITS <= 31, unsigned int,
typename std::conditional<BITS <= 63, unsigned long,
typename std::conditional<BITS <= 127, unsigned long long,
unsigned long long[]>::type>::type>::type>::type;
WORDTYPE data;
// ...
};
```
A good start, but withh two bugs, a missing extra '>::type' and the array really wants to be a whole new template specialization with WORDTYPE :-
unsigned long long[(BITS+127)/128]
> I am looking for solutions both with and without concepts for comparison, if its possible without and its simpler I will use that solution unless there is some real advantages to using concepts.
I am thinking concepts may be necessary ?
If you would like to help on the journey it would be well appreciated :)
Aaron