Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

generate consecutive numbers at compile time

template<size_t n>
struct Elem{
    enum { res = 1 + Elem<n - 1>::res };
};

template<>
struct Elem<0> {
    enum { res = 0 };
};
template <size_t n, size_t...args>
struct A {
    static constexpr auto& array =
        A<n - 1, Elem<n>::res, args...>::array;
};
template <size_t...args>
struct A<0, args...> {
    static constexpr int array[] = { 0, args... };
};

template <size_t n>
static constexpr auto& Array = A<n>::array;

//пример использования
int main()
{   
    constexpr unsigned N = 8;
    for (unsigned n : Array<N>)
        cout << n << ' ';
    //0 1 2 3 4 5 6 7 8
}
Source by ru.stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #generate #consecutive #numbers #compile #time
ADD COMMENT
Topic
Name
2+1 =