采用DenseBase <Derived>的非常量引用并将其填充到函数中不起作用

I am writing a function to sort dense matrices or arrays in Eigen and also return the indices of the arrangement. The function is the following:

template<typename Derived1, typename Derived2>
typename Derived1::PlainObject sort(const DenseBase<Derived1> &x, DenseBase<Derived2> &indices)
{
    typename Derived1::PlainObject y = x.derived();
    typename Derived2::PlainObject z = indices.derived();

    z.resize(y.rows(), y.cols());
    for (int i = 0; i < z.size(); ++i)
        z(i) = i;

    std::sort(z.data(), z.data() + z.size(), [&](size_t a, size_t b) { return y(a) < y(b); });

    for (int i = 0; i < z.size(); ++i)
        y(i) = x((int) z(i));

    return y;
}

现在,我想以以下方式在一些代码中调用此函数:

const ArrayXXd x = ArrayXXd::Random(5, 5);
ArrayXXi indices;
const ArrayXXd xsort = sort(x, indices);

x is sorted correctly but I expected the indices matrix/array to hold the indices of the sorting process, but it is empty :|

What happens here? In the first function z (which is the underlying derived type of indices) is correctly allocated and filled, why after the function ends indices is empty?

非常感谢你。