How to extract the highest-indexed specialization from a structure?

亡梦爱人 提交于 2019-12-05 01:33:39

This is the first version which gets you the maximum index for which specialization is defined. From this, you will get the corresponding type!

Implementation:

template<class T>
struct highest_index
{
  private:
     template<int i>
     struct is_defined {};

     template<int i>
     static char f(is_defined<sizeof(typename T::template D<i>)> *);

     template<int i>
     static int f(...);

     template<int i>
     struct get_index;

     template<bool b, int j>
     struct next
     {
        static const int value = get_index<j>::value;
     };
     template<int j>
     struct next<false, j>
     {
        static const int value = j-2;
     };
     template<int i>
     struct get_index
     {
        static const bool exists = sizeof(f<i>(0)) == sizeof(char);
        static const int value = next<exists, i+1>::value;
     };

    public:
     static const int index = get_index<0>::value; 
};

Test code:

#include <iostream>

struct A
{
    template<unsigned int> struct D;
};
template<> struct A::D<0> { };
template<> struct A::D<1> { };

struct B
{
    template<unsigned int> struct D;
};
template<> struct B::D<0> { };
template<> struct B::D<1> { };
template<> struct B::D<2> { };


int main()
{
    std::cout << highest_index<A>::index << std::endl;
    std::cout << highest_index<B>::index << std::endl;
}

Output:

1
2

Live demo. :-)

Figured it out with the help from the comments under the question!

struct A { template<unsigned int> struct D; };
template<> struct A::D<0> { };

struct B { template<unsigned int> struct D; };
template<> struct B::D<0> { };
template<> struct B::D<1> { };

struct C { template<unsigned int> struct D; };
template<> struct C::D<0> { };
template<> struct C::D<1> { };
template<> struct C::D<2> { };
template<> struct C::D<3> { };

template<unsigned int>
static unsigned char test(...);

template<unsigned int N, class T>
static typename enable_if<
    sizeof(typename T::template D<N>),
    unsigned char (&)[1 + sizeof(test<N + 1>(T()))]
>::type test(T, typename T::template D<N> = typename T::template D<N>());

int main()
{
    return sizeof(test<0>(C())) - 1;  // Evaluates to number of specializations
}

Here is my little contribution.

We start off with the existence methods:

template <unsigned>
static unsigned char exists_impl(...);

template <unsigned N, typename T>
static auto exists_impl(T const&&) ->
    typename std::enable_if<sizeof(typename T::template D<N>),
                            unsigned char (&)[2]>::type;

template <typename T, unsigned N>
static constexpr bool exists() {
    return sizeof(exists_impl<N>(std::declval<T>())) != 1;
}

I believe here that constexpr and function usage do bring a lot to the table in terms of readability, so I don't use the typical types.

Then, we use a typical binary search (2nd attempt, see first attempt at bottom), at a loss of readability, but to benefit from lazy instantiation, we use partial template specialization and std::conditional:

template <typename T, unsigned low, unsigned high, typename = void>
struct highest_index_in;

template <typename T, unsigned low>
struct highest_index_in<T, low, low>: std::integral_constant<unsigned, low> {};

template <typename T, unsigned low, unsigned high>
struct highest_index_in<T, low, high, typename std::enable_if<(high == low + 1)>::type>:
  std::integral_constant<unsigned, low + exists<T, low+1>()> {};

template <typename T, unsigned low, unsigned high>
struct highest_index_in<T, low, high, typename std::enable_if<(high > low + 1)>::type>:
  std::conditional< exists<T, (low+high)/2>(),
                    highest_index_in<T, (low+high)/2, high>,
                    highest_index_in<T, low, (low+high)/2> >::type
{};

template <typename T>
static constexpr unsigned highest_index() {
   return highest_index_in<T, 0, ~(0u)>::value;
} // highest_index

Demo at liveworkspace, computing highest_index<C>() is near instantaneous.


First attempt at binary search, unfortunately the compiler need instantiate the function bodies recursively (to prove they can be instantiated) and thus the work it has to do is tremendous:

template <typename T, unsigned low, unsigned high>
static constexpr auto highest_index_in() ->
   typename std::enable_if<high >= low, unsigned>::type
{
   return low == high                 ? low :
          high == low + 1             ? (exists<T, high>() ? high : low) :
          exists<T, (high + low)/2>() ? highest_index_in<T, (high+low)/2, high>() :
                                        highest_index_in<T, low, (high+low)/2>();
} // highest_index_in

template <typename T>
static constexpr unsigned highest_index() {
   return highest_index_in<T, 0, ~(0u)>();
} // highest_index

So, unfortunately, highest_index is not usable and the clang is dang slow (not that gcc appears to be doing better).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!