Compile-time-fixed templated integer range This is a follow-up of an old question by @LokiAstari, modified for the current community challenge. The idea is to provide a compile-time integer range. I applied all the modifications that I proposed to Loki at the time and tried to write a class as close as possible to the standard library class std::integer_sequence:


This is a [begin, end) range instead of a [begin, end] one.
The range can be ascending or descending.
The class is templated so that it is possible to choose the integer type to use.
In order to match the standard library utilities, I also provide the template index_range which is an alias of integer_range for the type std::size_t.




Here is the implementation:

#include &lt;cstddef&gt;
#include &lt;tuple&gt;
#include &lt;type_traits&gt;
#include &lt;utility&gt;

namespace details
{
    ////////////////////////////////////////////////////////////
    // implementation details

    template&lt;typename Int, typename, Int Begin&gt;
    struct increasing_integer_range;

    template&lt;typename Int, Int... N, Int Begin&gt;
    struct increasing_integer_range&lt;Int, std::integer_sequence&lt;Int, N...&gt;, Begin&gt;:
        std::integer_sequence&lt;Int, N+Begin...&gt;
    {};

    template&lt;typename Int, typename, Int Begin&gt;
    struct decreasing_integer_range;

    template&lt;typename Int, Int... N, Int Begin&gt;
    struct decreasing_integer_range&lt;Int, std::integer_sequence&lt;Int, N...&gt;, Begin&gt;:
        std::integer_sequence&lt;Int, Begin-N...&gt;
    {};
}

////////////////////////////////////////////////////////////
// integer_range

template&lt;typename Int, Int Begin, Int End,
         bool Increasing=(Begin&lt;End)&gt;
struct integer_range;

template&lt;typename Int, Int Begin, Int End&gt;
struct integer_range&lt;Int, Begin, End, true&gt;:
    details::increasing_integer_range&lt;
        Int,
        std::make_integer_sequence&lt;Int, End-Begin&gt;,
        Begin&gt;
{};

template&lt;typename Int, Int Begin, Int End&gt;
struct integer_range&lt;Int, Begin, End, false&gt;:
    details::decreasing_integer_range&lt;
        Int,
        std::make_integer_sequence&lt;Int, Begin-End&gt;,
        Begin&gt;
{};

template&lt;std::size_t Begin, std::size_t End&gt;
using index_range = integer_range&lt;std::size_t, Begin, End&gt;;


I don't ask for a specific kind of review. If you see anything that can be improved, do not hesitate.



And as a bonus, take this test cases. Since integer_range uses some heavy template wizardry, I had troubles coming with decent test cases that are easy enough to read, write and understand. They are not really beautiful, but they work as expected; that may help you if you want to try modifications:

template&lt;std::size_t N&gt;
void test(std::integer_sequence&lt;int&gt;)
{
    static_assert(N == 0, "");
}

template&lt;std::size_t N&gt;
void test(std::integer_sequence&lt;int, 0, 1, 2, 3, 4&gt;)
{
    static_assert(N == 1, "");
}

template&lt;std::size_t N&gt;
void test(std::integer_sequence&lt;int, 5, 4, 3, 2, 1&gt;)
{
    static_assert(N == 2, "");
}

template&lt;std::size_t N&gt;
void test(std::integer_sequence&lt;int, -3, -2, -1, 0, 1&gt;)
{
    static_assert(N == 3, "");
}

template&lt;std::size_t N&gt;
void test(std::integer_sequence&lt;int, -1, -2, -3&gt;)
{
    static_assert(N == 4, "");
}

template&lt;std::size_t N&gt;
void test(std::integer_sequence&lt;int, 3, 2, 1, 0, -1, -2, -3, -4, -5, -6, -7&gt;)
{
    static_assert(N == 5, "");
}

int main()
{
    using namespace polder;

    test&lt;0&gt;(integer_range&lt;int, 8, 8&gt;());
    test&lt;1&gt;(integer_range&lt;int, 0, 5&gt;());
    test&lt;2&gt;(integer_range&lt;int, 5, 0&gt;());
    test&lt;3&gt;(integer_range&lt;int, -3, 2&gt;());
    test&lt;4&gt;(integer_range&lt;int, -1, -4&gt;());
    test&lt;5&gt;(integer_range&lt;int, 3, -8&gt;());
}




Note: if you're interested, I wrote a follow-up to this question which incorporates most of the suggestions from the accepted answer and adds the possibility to specify a Step parameter.
IMO, your test cases would be vastly more readable if you wrote them simply in terms of std::is_same. If you were going to write a lot of test cases, you could wrap all that boilerplate up in a macro such as

#define TEST(type, start, end, ...) \
    static_assert(std::is_same&lt; \
        polder::integer_range&lt;type, start, end&gt;, \
        std::integer_sequence&lt;type ,##__VA_ARGS__ &gt; &gt;::value)


which reduces your test cases to the tiny amount of code they actually are.

TEST(int, 8,8);
TEST(int, 0,5, 0,1,2,3,4);
TEST(int, 5,0, 5,4,3,2,1);
TEST(int, -3,2, -3,-2,-1,0,1);
TEST(int, -1,-4, -1,-2,-3);
TEST(int, 3,-8, 3,2,1,0,-1,-2,-3,-4,-5,-6,-7);


This shorter formulation incidentally makes it more obvious that you didn't test any corner cases or types other than int — which is why I'm a strong proponent of "shorter code is better code." We've now made the code short enough that we can see where the bugs are.

Okay, let's move on to looking at the actual code. It actually looks very clean and simple. Admittedly I did think at first that you were doing the bad O(n) approach where integer_range&lt;N&gt; inherits from integer_range&lt;N-1&gt;; it took me a moment to see that you were in fact doing the right thing. I see only two issues with

template&lt;typename Int, Int... N, Int Begin&gt;
struct increasing_integer_range&lt;Int, std::integer_sequence&lt;Int, N...&gt;, Begin&gt;:
    std::integer_sequence&lt;Int, N+Begin...&gt;
{};


The first problem is that our new test cases don't pass, because your increasing_integer_range is not actually a synonym for std::integer_sequence; instead it's merely derived from std::integer_sequence. This is good enough for the average codebase, but for a library implementation we'd want to make sure that the two types are actually equal. Fortunately, C++11 template typedefs make this easy. Rewrite it as

namespace details
{
    template&lt;typename Int, typename, Int Begin, bool Increasing&gt;
    struct integer_range_impl;

    template&lt;typename Int, Int... N, Int Begin&gt;
    struct integer_range_impl&lt;Int, std::integer_sequence&lt;Int, N...&gt;, Begin, true&gt; {
        using type = std::integer_sequence&lt;Int, N+Begin...&gt;;
    };

    template&lt;typename Int, Int... N, Int Begin&gt;
    struct integer_range_impl&lt;Int, std::integer_sequence&lt;Int, N...&gt;, Begin, false&gt; {
        using type = std::integer_sequence&lt;Int, Begin-N...&gt;;
    };
}

template&lt;typename Int, Int Begin, Int End&gt;
using integer_range = typename details::integer_range_impl&lt;
    Int,
    std::make_integer_sequence&lt;Int, (Begin&lt;End) ? End-Begin : Begin-End&gt;,
    Begin,
    (Begin&lt;End) &gt;::type;

template&lt;std::size_t Begin, std::size_t End&gt;
using index_range = integer_range&lt;std::size_t, Begin, End&gt;;


The second problem is that this implementation does not work for some obvious corner cases, such as integer_range&lt;int, INT_MIN, INT_MAX&gt;, because in general the result of End-Begin is not guaranteed to be representable in Int.

Fixing the second problem is left as an exercise for the reader, because I believe it's pretty darn hard. I think you might have to abandon the (really clever) idea of using std::integer_sequence to do your heavy lifting, and resign yourself to reimplementing integer_sequence something like Xeo did here except harder.

EDIT TO ADD a third "problem" I just thought of: The name integer_range could be considered misleading. If I were proposing this for the standard library, I'd name it make_integer_range, by analogy with make_integer_sequence.