Declaring templated member reference to templated member array of another class

88 Views Asked by At

Template class OtherClass has a template member that is a reference to template class BitReferenceHost's template member, which is a std::array constructed using make_integer_sequence and two templated member methods of BitReferenceHost.

I have correctly declared XBitMask in BitReferenceHost, but I am having trouble in OtherClass where it uses a reference to the static member of BitReferenceHost.

#include <cstdint>
#include <array>
#include <limits>
#include <functional>

template <typename Aspect>
class BitReferenceHost {
    public:

    template <uint8_t BitCount, uint64_t... Indexes>
    static constexpr const std::array<uint64_t, sizeof...(Indexes)>
    initializeXBitMasks(const std::integer_sequence<uint64_t, Indexes...>&) {
        return { initializeXBitMasks<BitCount>( Indexes )... };
    }

    template<
        uint8_t   BitCount,
        uint8_t   ShiftDistance = BitCount + 1,
        uint64_t  BaseMask      = (1ULL << ShiftDistance) - 1
    > static constexpr const uint64_t initializeXBitMasks(
        const uint64_t& mask_index
    ) {
        return BaseMask << (BitCount * mask_index);
    }

    template <
        uint8_t   BitCount,
        typename  MaskInt = uint64_t,
        uint8_t   Count   = std::numeric_limits<MaskInt>::digits / BitCount
    > static constexpr const std::array <MaskInt, Count>
    XBitMask = initializeXBitMasks<BitCount>(
        std::make_integer_sequence<MaskInt, Count>{}
    );

};

template <typename Aspect>
template <uint8_t  BitCount,
  typename MaskInt,
  uint8_t  Count
> const std::array<MaskInt, Count> BitReferenceHost<Aspect>::XBitMask;

template <typename Aspect, typename... Args>
class OtherClass {
    public:
    using ReferenceHost = ::BitReferenceHost<Aspect>;

    template <uint8_t  BitMaskBitCount>
    static constexpr const auto& XBitMask =
            ReferenceHost::template XBitMask<BitMaskBitCount>;
};

template <typename Aspect, typename... Args>
template <uint8_t  BitMaskBitCount>
const auto& OtherClass<Aspect, Args...>::XBitMask;

Update (Solution):

Working code per answer provided by max66:

#include <cstdint>
#include <iostream>
#include <array>
#include <limits>
#include <functional>

template <typename  Aspect> class BitReferenceHost {
  template <uint8_t BitCount, uint64_t... Indexes>
  static constexpr const std::array<uint64_t, sizeof...(Indexes)>
  initializeXBitMasks( const std::integer_sequence<uint64_t, Indexes...>& )
  {
    return { { initializeXBitMasks<BitCount>( Indexes )... } };
  }
  template
  <
    uint8_t   BitCount,
    uint64_t  BaseMask      = (1ULL << BitCount) - 1
  >
  static constexpr uint64_t
  initializeXBitMasks(
    const uint64_t&  mask_index )
  {
    return BaseMask << (BitCount * mask_index);
  }

   public:
  template
  <
    uint8_t   BitCount,
    typename  MaskInt = uint64_t,
    uint8_t   Count   = std::numeric_limits<MaskInt>::digits / BitCount
  >
  static constexpr const std::array<MaskInt,Count>
  XBitMask = initializeXBitMasks<BitCount>( std::make_integer_sequence<MaskInt, Count>{} );
};

template<typename  Aspect>
template
<uint8_t BitCount, typename MaskInt, uint8_t Count>
const std::array<MaskInt, Count>
BitReferenceHost<Aspect>::XBitMask;

template <typename Aspect, typename... Args> class OtherClass
{
  using ReferenceHost = ::BitReferenceHost<Aspect>;
   public:
  template <uint8_t  BitMaskBitCount> static constexpr const decltype(ReferenceHost::template XBitMask<BitMaskBitCount>)&
  XBitMask = ReferenceHost::template XBitMask<BitMaskBitCount>;
};

template <typename Aspect, typename... Args>
template <uint8_t  BitMaskBitCount>
constexpr const decltype(OtherClass<Aspect, Args...>::ReferenceHost::template XBitMask<BitMaskBitCount>)&
OtherClass
<Aspect, Args...>::XBitMask;

int main()
{
  uint64_t test_int = OtherClass<uint64_t>::XBitMask<1>[0];

  std::cout << test_int << std::endl;

  return 0;
}
1

There are 1 best solutions below

7
On BEST ANSWER

I'm not an expert but... I have problem compiling your code because (if I understand well the error message from clang) the auto type in

template <typename Aspect, typename... Args>
template <uint8_t  BitMaskBitCount>
const auto& OtherClass<Aspect, Args...>::XBitMask;

can't be deduced because there isn't a initialization value.

I compile in two ways

(1) deleting the declaration outside the class definition

template <typename Aspect, typename... Args>
class OtherClass {
    using ReferenceHost = ::BitReferenceHost<Aspect>;

    template <uint8_t  BitMaskBitCount>
    static constexpr const auto& XBitMask =
            ReferenceHost::template XBitMask<BitMaskBitCount>;
};

//template <typename Aspect, typename... Args>
//template <uint8_t  BitMaskBitCount>
//const auto& OtherClass<Aspect, Args...>::XBitMask;

(2) avoiding auto for the type (and usign a using defined type to semplify)

template <typename Aspect, typename... Args>
class OtherClass {
    using ReferenceHost = ::BitReferenceHost<Aspect>;
    template <uint8_t  BitMaskBitCount>
    using xbit_t        = decltype(ReferenceHost::template XBitMask<BitMaskBitCount>); 

    template <uint8_t  BitMaskBitCount>
    static constexpr xbit_t<BitMaskBitCount> & XBitMask =
            ReferenceHost::template XBitMask<BitMaskBitCount>;
};

template <typename Aspect, typename... Args>
template <uint8_t  BitMaskBitCount>
constexpr OtherClass<Aspect, Args...>::xbit_t<BitMaskBitCount> & 
   OtherClass<Aspect, Args...>::XBitMask;