你好我正在学习 C++11,我想知道如何制作一个 constexpr 0 到 n 数组,例如:
n = 5;
int array[] = {0 ... n};
所以数组可能是{0, 1, 2, 3, 4, 5}
最佳答案
在 C++14 中,可以使用 constexpr
构造函数和循环轻松完成:
#include <iostream>
template<int N>
struct A {
constexpr A() : arr() {
for (auto i = 0; i != N; ++i)
arr[i] = i;
}
int arr[N];
};
int main() {
constexpr auto a = A<4>();
for (auto x : a.arr)
std::cout << x << '\n';
}
关于c++ - 在 C++11 中创建 N 元素 constexpr 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35101817/