对嵌套数组类型的常量变量的副作用

时间:2017-07-16 00:02:50

标签: c++ const side-effects

我遇到了一些奇怪的副作用,我无法用丝毫解释。可能我错过了一些非常明显的东西,但我现在已经找了几个小时的bug,而且代码非常简单,所以我得出结论,我必须对......有一些相当根本的误解。

考虑这个代码,用于计算两个2d矩阵的乘积(我已经更改了set()函数以将-1添加到参数单元格,以使调试输出更易于理解。 / p>

template<class T, unsigned column_count, unsigned row_count>
class Matrix
{
private:
    const static unsigned row_length    = column_count;
    const static unsigned column_length = row_count;
    using matrix_type = std::array<std::array<T, row_length>, row_count>;
    matrix_type matrix;

public:
    using value_type = T;

    Matrix(const matrix_type& matrix) : matrix(matrix) {}
    Matrix() {}

    friend std::ostream& operator<<(std::ostream& o, const Matrix& rhs)
    {
        for (unsigned i = 0; i < column_count; ++i) {
            for (unsigned j = 0; j < row_count; ++j) {
                o << rhs.matrix[i][j] << ' ';
            }
            o << '\n';
        }
        return o;
    }

    const auto& get_rows() const { return matrix; }

    const auto get_columns() const
    {
        std::array<std::array<T, column_length>, column_count> columns;

        for (unsigned i = 0; i < row_length; ++i) {
            for (unsigned j = 0; j < column_length; ++j) {
                columns[i][j] = matrix[j][i];
            }
        }
        return columns;
    }

    void set(unsigned i, unsigned j, T v) { matrix[i][j] = -1; }

    friend Matrix operator*(const Matrix& m1, const Matrix& m2)
    {

        auto columns = m1.get_columns();
        auto rows    = m2.get_rows();

        Matrix m3;

        std::cout << "before:"
                  << "\n";
        std::cout << m1 << "\n";
        std::cout << m2 << "\n";
        std::cout << m3 << "\n";

        unsigned i{ 0 };

        for (const auto& row : rows) {

            i++;
            unsigned j{ 0 };

            for (const auto& column : columns) {

                j++;
                value_type v{ 0 };

                for (unsigned k = 0; k < column.size(); ++k) {
                    v += row[k] * column[k];
                }
                m3.set(i, j, v);
            }
        }

        std::cout << "after:"
                  << "\n";
        std::cout << m1 << "\n";
        std::cout << m2 << "\n";
        std::cout << m3 << "\n";

        return m3;
    }
};

如您所见,getter函数要么返回副本,要么返回常量引用。 operator*函数采用常量参数。

我现在构建两个矩阵,如下:

std::array<int, 3> c1{ { 1, 2, 3 } };
std::array<int, 3> c2{ { 4, 5, 6 } };
std::array<int, 3> c3{ { 7, 8, 9 } };

std::array<std::array<int, 3>, 3> m1{ { c1, c2, c3 } };

std::array<std::array<int, 3>, 3> m2 = m1;

Matrix<int, 3, 3> matrix1(m1);
Matrix<int, 3, 3> matrix2(m2);

现在我以不同的方式调用operator*

matrix1* matrix2;

结果:

before:
1 2 3
4 5 6
7 8 9

1 2 3
4 5 6
7 8 9

0 0 0
0 0 0
0 0 183238709

after:
-1 -1 -1
4 5 6
7 8 9

1 2 3
4 5 6
7 8 9

0 0 0
0 -1 -1
-1 -1 -1

matrix2* matrix1;

结果:

before:
1 2 3
4 5 6
7 8 9

1 2 3
4 5 6
7 8 9

0 0 0
0 0 0
0 0 -1823473620

after:
1 2 3
4 5 6
7 8 9

-1 -1 -1
4 5 6
7 8 9

0 0 0
0 -1 -1
-1 -1 -1

matrix1* matrix1;

结果:

before:
1 2 3
4 5 6
7 8 9

1 2 3
4 5 6
7 8 9

1385085408 32767 401515081
1 1385085440 32767
1385085440 32767 1385085464

after:
-1 -1 -1
4 5 6
7 8 9

-1 -1 -1
4 5 6
7 8 9

1385085408 32767 401515081
1 -1 -1
-1 -1 -1

如您所见,作为第一个参数传递的矩阵将被更改。这对我来说毫无意义,因为它作为const传递,set()仅在m3上运行。不知怎的m3得到了部分&#34;绑定&#34;到作为operator*的第一个参数的矩阵。为什么呢?

1 个答案:

答案 0 :(得分:0)

您在循环中写出越界,因为ij都过早递增。

代码应如下所示:

for (const auto& row : rows) {
    // i++;
    unsigned j{ 0 };

    for (const auto& column : columns) {
        // j++;
        value_type v{ 0 };

        for (unsigned k = 0; k < column.size(); ++k) {
            v += row[k] * column[k];
        }
        m3.set(i, j, v);

        j++; // <--
    }

    i++; // <--
}