For completeness' sake, here's a version that does not require the definition of a function but instead uses a lambda. C++17 introduced the ability of using lambdas in constant expressions, so you can declare your array constexpr
and use a lambda to initialize it:
static constexpr auto axis = [] {
std::array<double, num_points> a{};
for (int i = 0; i < num_points; ++i) {
a[i] = 180 + 0.1 * i;
}
return a;
}();
(Note the ()
in the last line, which calls the lambda right away.)
If you don't like the auto
in the axis
declaration because it makes it harder to read the actual type, but you don't want to repeat the type inside the lambda, you can instead do:
static constexpr std::array<double, num_points> axis = [] {
auto a = decltype(axis){};
for (int i = 0; i < num_points; ++i) {
a[i] = 180 + 0.1 * i;
}
return a;
}();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…