10

我正在尝试进行一些模板元编程,并且我发现需要“提取”某种类型的某些结构的专业化的最高索引。

例如,如果我有一些类型:

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

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

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

然后我怎样才能编写这样的元函数:

template<class T>
struct highest_index
{
    typedef ??? type;
    // could also be:   static size_t const index = ???;
};

给我在D上述任意结构中专门化的最高索引,而不需要结构明确声明计数?

4

3 回答 3

4

这是第一个为您提供定义专业化的最大索引的版本。从中,你会得到相应的类型!

执行:

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; 
};

测试代码:

#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;
}

输出:

1
2

现场演示。:-)

于 2013-01-08T09:31:03.490 回答
2

在问题下的评论的帮助下弄清楚了!

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
}
于 2013-01-08T09:37:41.933 回答
1

这是我的一点贡献。

我们从存在方法开始:

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;
}

我相信这里constexpr和函数的使用确实在可读性方面带来了很多好处,所以我不使用典型的类型。

然后,我们使用典型的二分搜索(第二次尝试,见底部的第一次尝试),虽然失去了可读性,但为了从惰性实例化中受益,我们使用部分模板特化和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

liveworkspace演示,计算highest_index<C>()几乎是瞬时的。


第一次尝试二进制搜索,不幸的是编译器需要递归地实例化函数体(以证明它们可以被实例化),因此它必须做的工作是巨大的:

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

所以,不幸的是,highest_index它不可用,并且clang很慢(并不是说gcc似乎做得更好)。

于 2013-01-08T11:07:39.657 回答