I have a C++ class with a static constexpr array. I'd like to assign it equal to another class member function. Right now I have the current solution, but I feel it's pretty suboptimal, and the function is no longer scoped to the class.
static constexpr std::array<Degrees, 270> angles()
{
std::array<Degrees, 270> angles;
for (size_t i = 0; i < angles.size(); i++)
{
angles[i] = static_cast<double>(i - 90.0) * (std::numbers::pi / 180.0);
};
return angles;
}
class MyClass
{
static constexpr std::array<Degrees, 270> angles = angles();
}
Is there a better approach for initialising a static constexpr class member to a function like this? Ideally I'd do this using a member function of MyClass, with the implementation defined in the .cpp instead of the header.
constexprcode from the public header file.