Search
 
SCRIPT & CODE EXAMPLE
 

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
}
Comment

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
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: c++ program 
Cpp :: C:UsersBBCDocumentsc n c++ project8PuzzleSolvemain.c|38|warning: suggest parentheses around assignment used as truth value [-Wparentheses]| 
Cpp :: interactive problem 
Cpp :: Patrick and Shopping codeforces in c++ 
Cpp :: 130 divided by -10 
Cpp :: private static c++ 
Cpp :: lnk2001 unresolved external symbol __imp_PlaySoundA 
Cpp :: compilling c++ and c by console 
Cpp :: split the array there is an array val of n integers . A good subarray is defined as 
Cpp :: void linux java 
Cpp :: the partition function of a system is given by z= 1/(1-e^-bEi), calculate the total energy of the system 
Cpp :: +++++++++ 
Cpp :: graph colouring backtracking 
Cpp :: find the second aperrence of a char in string c++ 
Cpp :: C++ Point to Every Array Elements 
Cpp :: Required Length 
Cpp :: get array size 
Cpp :: split date and time in a column in db browser 
Cpp :: c++ coding questions for interview 
Cpp :: online computer graphics compiler c++ 
Cpp :: c++ max and min of vector 
Cpp :: middle node of linked list 
Cpp :: longest increasing subsequence nlogn c++ 
Cpp :: Arduino Counting 
Cpp :: C++ mutex header 
C :: csrf_exempt 
C :: .gitkeep file 
C :: imprimir valor no octave 
C :: get chunks of a mp4 in ffmpeg 
C :: how to remove from a string c 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =