1

I am trying to convert the following array of bit masks to a more compact version.

template < typename T >
constexpr T length_mask[];

template <>
constexpr uint64 length_mask < uint64 >[] = {
  uint64(0)
, ~uint64(0) >> 56
, ~uint64(0) >> 48
, ~uint64(0) >> 40
, ~uint64(0) >> 32
, ~uint64(0) >> 24
, ~uint64(0) >> 16
, ~uint64(0) >> 8
, ~uint64(0) >> 0
};

Here is what I have...

template < typename T, typename I >
constexpr T length_mask_impl[];

template < typename T, std::size_t... I >
constexpr T length_mask_impl < T, std::index_sequence < I... > >[] = {
  ~T(0), (~T(0) >> ((sizeof(T) - I - 1) * 8))...
};

I wanted to further simplify this for the client code, so they don't have to write out std::make_index_sequence. How can I do that? I am unable to get the syntax right and not sure if this would work either.

template < typename T >
using length_mask < T > = length_mask_impl < T, std::make_index_sequence < sizeof(T) > >;

That doesn't compile. What should the declaration/definition of length_mask be?

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
zrb
  • 851
  • 7
  • 16
  • `template auto length_mask = ..` – David G Jul 15 '15 at 00:53
  • 2
    But ... _why_. You're not going to make it more legible. Nor faster. Infact I'd even step back one bit further and write it `... = { 0x0000'0000'0000'0000UL, 0x0000'0000'0000'000FFUL, ....}` So its blindingly obvious what your masks are. - – Michael Anderson Jul 15 '15 at 01:09
  • 0x499602D2.. that comes up with the following error on gcc 5.1 error: invalid conversion from ‘const int64_t unsigned*’ to ‘int64_t unsigned’ [-fpermissive] auto length_mask = length_mask_t < T, std::make_index_sequence < sizeof(T) > >; – zrb Jul 15 '15 at 08:42
  • Michael... just trying to explore what is possible – zrb Jul 15 '15 at 08:42

0 Answers0