实现C ++ 14 make_integer_sequence [英] Implementation C++14 make_integer_sequence
问题描述
我尝试实施 C ++ 14 < a>别名模板 make_integer_sequence
,它简化了类模板的创建 integer_sequence
。
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
:
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编译程序,在四核i5系统具有8GB的RAM。
成功编译需要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:
虚拟内存耗尽。
virtual memory exhausted.
有人可以解释这个错误,导致了?
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:
虚拟内存耗尽。
virtual memory exhausted.
所以,我的问题是,如何减少模板深度实例化?
So, my question is, how do I decrease template deep instantiation?
尊敬的,
Khurshid。
Regards, Khurshid.
推荐答案
这里是 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屋!