Swap operation error: binding value of type 'const vector<...>' to reference to type 'vector<...>' drops 'const' qualifier

Swap operation error: binding value of type 'const vector<...>' to reference to type 'vector<...>' drops 'const' qualifier

我有一个 class 方法 returns const std::vector:

class TriangleMesh
{
public:
    const std::vector<Vec3i>& indices()  const { return m_indices; };
private:
    std::vector<Vec3i> m_indices;
};

我正在通过另一个结构调用上述方法并执行 swap 操作:

struct Contour3D {
    std::vector<Vec3i> faces3;
    
    // ...
    Contour3D(TriangleMesh &&trmesh);
    
};

Contour3D::Contour3D(TriangleMesh &&trmesh)
{
    faces3.swap(trmesh.indices()); // => error: binding value of type 'const vector<...>' to reference to type 'vector<...>' drops 'const' qualifier
}

但我在 swap 语句中收到此错误:

error: binding value of type 'const vector<...>' to reference to type 'vector<...>' drops 'const' qualifier

我不知道如何避免上述错误。

一个选项

一种选择是使用循环来避免swap。到目前为止,我想出了这个循环,但我觉得我错过了一些东西。我在这里缺少什么:

Contour3D::Contour3D(TriangleMesh &&trmesh)
{
    faces3.reserve(trmesh.indices().size());

    std::copy(trmesh.indices().begin(), trmesh.indices().end(),
              std::back_inserter(faces3));
}

感谢@Yksisarvinen 这个声明工作正常:

faces3 = trmesh.indices();