[libcxx] r185343 - Implement n3658 - Compile-time integer sequences

Howard Hinnant hhinnant at apple.com
Mon Jul 1 15:17:00 PDT 2013


On Jul 1, 2013, at 3:45 PM, Howard Hinnant <hhinnant at apple.com> wrote:

>> 
>> This is O(n^2). You can do it in O(n log n):
>> 
>> template<typename T, T N, T Parity> struct __make_integer_sequence;
>> template<typename T, T N> using make_integer_sequence =
>> __make_integer_sequence<T, N, N % 2>::type;
>> 
>> template<typename T> struct __make_integer_sequence<T, 0, 0> { typedef
>> integer_sequence<T> type; };
>> template<typename T> struct __make_integer_sequence<T, 1, 1> { typedef
>> integer_sequence<T, 0> type; };
>> 
>> template<typename T, T ...Extra> struct __make_integer_sequence_impl;
>> template<typename T, T ...N, T ...Extra> struct
>> __make_integer_sequence_impl<integer_sequence<T, N...>, Extra...> {
>> typedef integer_sequence<T, N..., sizeof...(N) + N..., Extra...> type;
>> };
>> 
>> template<typename T, T N> struct __make_integer_sequence<T, N, 0> {
>> typedef __make_integer_sequence_impl<make_integer_sequence<T, N /
>> 2>>::type type;
>> };
>> template<typename T, T N> struct __make_integer_sequence<T, N, 1> {
>> typedef __make_integer_sequence_impl<make_integer_sequence<T, N /
>> 2>, N>::type type;
>> };
> 
> This is an interesting suggestion, thanks.  I think it has the potential to make make_integer_sequence much more robust against exceeding recursive template instantiation limits.  However as suggested, the above doesn't compile.  After working with it for a while I got it to compile, but then it didn't give the desired results.

Inspired by your approach, I started from scratch and came up with the enclosed.  The compiler will run out of memory before it runs into the recursive template instantiation limit, and this works with far higher numbers.  It passes all the current intseq tests.

Howard
-------------- next part --------------
A non-text attachment was scrubbed...
Name: make_integer_seq.patch
Type: application/octet-stream
Size: 2542 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130701/07be79e0/attachment.obj>


More information about the cfe-commits mailing list