Home >Backend Development >C++ >How to Initialize a C 11 `constexpr` Array from 0 to N?
In C 11, creating a constexpr array that spans from 0 to a specified integer n requires a bit more effort than in later C versions. Here's how it can be done:
Using a Constexpr Constructor and a Loop:
#include <iostream> template<int N> struct Array { constexpr Array() : arr() { for (auto i = 0; i != N; ++i) arr[i] = i; } int arr[N]; }; int main() { constexpr auto a = Array<5>(); for (auto x : a.arr) std::cout << x << '\n'; }
In this code:
The above is the detailed content of How to Initialize a C 11 `constexpr` Array from 0 to N?. For more information, please follow other related articles on the PHP Chinese website!