实现 C++14 make_integer_sequence [英] Implementation C++14 make_integer_sequence

查看:36
本文介绍了实现 C++14 make_integer_sequence的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试实现 C++14别名模板make_integer_sequence,它简化了类模板integer_sequence<的创建/code>.

I tried to implement the C++14 alias template make_integer_sequence, which simplifies the creation of the class template integer_sequence.

template< class T, T... I> struct integer_sequence
{
    typedef T value_type;
    static constexpr size_t size() noexcept { return sizeof...(I) ; }

};

template< class T, T N>
using make_integer_sequence = integer_sequence< T, 0,1,2, ... ,N-1 >; // only for illustration.

要实现make_integer_sequence,我们需要一个辅助结构make_helper.

To implement make_integer_sequence we need a helper structure make_helper.

template< class T , class N >
using make_integer_sequence = typename make_helper<T,N>::type;

实现 make_helper 并不太难.

template< class T, T N, T... I >
struct make_helper
{
   typedef typename mpl::if_< T(0) == N,  
                  mpl::identity< integer_sequence<T,I...> >,
                  make_helper< T, N-1, N-1,I...> 
               >::type;
};

为了测试 make_integer_sequence 我做了这个主要功能:

To test make_integer_sequence I made this main function:

int main()
{
    #define GEN(z,n,temp)   
     typedef make_integer_sequence< int, n >  BOOST_PP_CAT(int_seq,n) ;

   BOOST_PP_REPEAT(256, GEN, ~);
}

我使用 GCC 4.8.0 在具有 8GB RAM 的四核 i5 系统上编译了该程序.编译成功耗时 4 秒.

I compiled the program with GCC 4.8.0, on a quad-core i5 system with 8GBs of RAM. Successful compilation took 4 seconds.

但是,当我将 GEN 宏更改为:

But, when I changed the GEN macro to:

int main() {

#define GEN(z,n,temp) 
typedef make_integer_sequence< int, n * 4 > BOOST_PP_CAT(int_seq, n) ;

BOOST_PP_REPEAT(256, GEN, ~ );
}

编译不成功,输出错误信息:

The compilation was unsuccessful and outputted the error message:

虚拟内存耗尽.

有人能解释一下这个错误是什么导致的吗?

Could somebody explain this error and what caused it?

我将测试简化为:

int main()
{
   typedef make_integer_sequence< int, 4096 > int_seq4096;
}

然后我用 GCC 4.8.0 -ftemplate-depth=65536 成功编译.

I then successfully compiled with GCC 4.8.0 -ftemplate-depth=65536.

然而这第二个测试:

int main()
{
    typedef make_integer_sequence< int, 16384 > int_seq16384;
}

未使用 GCC 4.8.0 -ftemplate-depth=65536 编译,导致错误:

Did not compile with GCC 4.8.0 -ftemplate-depth=65536, and resulted in the error:

虚拟内存耗尽.

那么,我的问题是,如何减少模板深度实例化?

So, my question is, how do I decrease template deep instantiation?

问候,库尔希德.

推荐答案

这是一个 log N 实现,它甚至不需要增加模板实例化的最大深度并且编译速度非常快:

Here's a log N implementation that doesn't even need an increased max-depth for template instantiations and compiles pretty fast:

// using aliases for cleaner syntax
template<class T> using Invoke = typename T::type;

template<unsigned...> struct seq{ using type = seq; };

template<class S1, class S2> struct concat;

template<unsigned... I1, unsigned... I2>
struct concat<seq<I1...>, seq<I2...>>
  : seq<I1..., (sizeof...(I1)+I2)...>{};

template<class S1, class S2>
using Concat = Invoke<concat<S1, S2>>;

template<unsigned N> struct gen_seq;
template<unsigned N> using GenSeq = Invoke<gen_seq<N>>;

template<unsigned N>
struct gen_seq : Concat<GenSeq<N/2>, GenSeq<N - N/2>>{};

template<> struct gen_seq<0> : seq<>{};
template<> struct gen_seq<1> : seq<0>{};

这篇关于实现 C++14 make_integer_sequence的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆