How to initialize a matrix ONCE in a constexpr constructor?

P1331 (Permitting trivial default initialization in constexpr contexts) was adopted for C++20. It removes the requirement that:

every non-variant non-static data member and base class subobject shall be initialized

which is what required you to have the : data {} initialization.

This should just work:

template<size_t Rows, size_t Cols>
class matrix
{
    float data[Rows][Cols];
public:
    constexpr matrix(const float (&input)[Rows][Cols])
    {
        for (size_t i = 0; i < Rows; ++i)
            std::copy(input[i], input[i] + Cols, data[i]);
    }
};

No need to initialize data anymore.

Tags:

C++

C++20