From 57413492948d4c9dd6572f5bde6720b80122054a Mon Sep 17 00:00:00 2001 From: Gael Guennebaud Date: Tue, 7 Oct 2014 18:29:28 +0200 Subject: [PATCH] bug #882: fix various const-correctness issues with *View classes. --- Eigen/src/CholmodSupport/CholmodSupport.h | 2 +- Eigen/src/Core/CwiseUnaryView.h | 5 +++-- Eigen/src/Core/SelfAdjointView.h | 4 +++- Eigen/src/Core/TriangularMatrix.h | 18 ++++++++---------- Eigen/src/SparseCholesky/SimplicialCholesky.h | 8 ++++---- Eigen/src/SparseCore/SparseMatrixBase.h | 4 ++-- Eigen/src/SparseCore/SparseSelfAdjointView.h | 4 ++-- Eigen/src/SparseCore/SparseTriangularView.h | 4 ++-- 8 files changed, 25 insertions(+), 24 deletions(-) diff --git a/Eigen/src/CholmodSupport/CholmodSupport.h b/Eigen/src/CholmodSupport/CholmodSupport.h index 0c3b86dd2..3eadb83a0 100644 --- a/Eigen/src/CholmodSupport/CholmodSupport.h +++ b/Eigen/src/CholmodSupport/CholmodSupport.h @@ -105,7 +105,7 @@ const cholmod_sparse viewAsCholmod(const SparseMatrix<_Scalar,_Options,_Index>& /** Returns a view of the Eigen sparse matrix \a mat as Cholmod sparse matrix. * The data are not copied but shared. */ template -cholmod_sparse viewAsCholmod(const SparseSelfAdjointView, UpLo>& mat) +cholmod_sparse viewAsCholmod(const SparseSelfAdjointView, UpLo>& mat) { cholmod_sparse res = viewAsCholmod(mat.matrix().const_cast_derived()); diff --git a/Eigen/src/Core/CwiseUnaryView.h b/Eigen/src/Core/CwiseUnaryView.h index 61fd8ee35..6384dfdc3 100644 --- a/Eigen/src/Core/CwiseUnaryView.h +++ b/Eigen/src/Core/CwiseUnaryView.h @@ -37,7 +37,8 @@ struct traits > typedef typename MatrixType::Nested MatrixTypeNested; typedef typename remove_all::type _MatrixTypeNested; enum { - Flags = traits<_MatrixTypeNested>::Flags & (RowMajorBit | LvalueBit | DirectAccessBit), // FIXME DirectAccessBit should not be handled by expressions + FlagsLvalueBit = is_lvalue::value ? LvalueBit : 0, + Flags = traits<_MatrixTypeNested>::Flags & (RowMajorBit | FlagsLvalueBit | DirectAccessBit), // FIXME DirectAccessBit should not be handled by expressions MatrixTypeInnerStride = inner_stride_at_compile_time::ret, // need to cast the sizeof's from size_t to int explicitly, otherwise: // "error: no integral type can represent all of the enumerator values @@ -63,7 +64,7 @@ class CwiseUnaryView : public CwiseUnaryViewImpl::type NestedExpression; - explicit inline CwiseUnaryView(const MatrixType& mat, const ViewOp& func = ViewOp()) + explicit inline CwiseUnaryView(MatrixType& mat, const ViewOp& func = ViewOp()) : m_matrix(mat), m_functor(func) {} EIGEN_INHERIT_ASSIGNMENT_OPERATORS(CwiseUnaryView) diff --git a/Eigen/src/Core/SelfAdjointView.h b/Eigen/src/Core/SelfAdjointView.h index f5fbd7215..1c44d9c9a 100644 --- a/Eigen/src/Core/SelfAdjointView.h +++ b/Eigen/src/Core/SelfAdjointView.h @@ -38,7 +38,8 @@ struct traits > : traits typedef typename MatrixType::PlainObject FullMatrixType; enum { Mode = UpLo | SelfAdjoint, - Flags = MatrixTypeNestedCleaned::Flags & (HereditaryBits) + FlagsLvalueBit = is_lvalue::value ? LvalueBit : 0, + Flags = MatrixTypeNestedCleaned::Flags & (HereditaryBits|FlagsLvalueBit) & (~(PacketAccessBit | DirectAccessBit | LinearAccessBit)) // FIXME these flags should be preserved }; }; @@ -95,6 +96,7 @@ template class SelfAdjointView EIGEN_DEVICE_FUNC inline Scalar& coeffRef(Index row, Index col) { + EIGEN_STATIC_ASSERT_LVALUE(SelfAdjointView); Base::check_coordinates_internal(row, col); return m_matrix.const_cast_derived().coeffRef(row, col); } diff --git a/Eigen/src/Core/TriangularMatrix.h b/Eigen/src/Core/TriangularMatrix.h index 263d54485..055ed7514 100644 --- a/Eigen/src/Core/TriangularMatrix.h +++ b/Eigen/src/Core/TriangularMatrix.h @@ -173,7 +173,8 @@ struct traits > : traits typedef MatrixType ExpressionType; enum { Mode = _Mode, - Flags = (MatrixTypeNestedCleaned::Flags & (HereditaryBits | LvalueBit) & (~(PacketAccessBit | DirectAccessBit | LinearAccessBit))) + FlagsLvalueBit = is_lvalue::value ? LvalueBit : 0, + Flags = (MatrixTypeNestedCleaned::Flags & (HereditaryBits | FlagsLvalueBit) & (~(PacketAccessBit | DirectAccessBit | LinearAccessBit))) }; }; } @@ -213,7 +214,7 @@ template class TriangularView // FIXME This, combined with const_cast_derived in transpose() leads to a const-correctness loophole EIGEN_DEVICE_FUNC - explicit inline TriangularView(const MatrixType& matrix) : m_matrix(matrix) + explicit inline TriangularView(MatrixType& matrix) : m_matrix(matrix) {} using Base::operator=; @@ -229,14 +230,9 @@ template class TriangularView const NestedExpression& nestedExpression() const { return m_matrix; } EIGEN_DEVICE_FUNC NestedExpression& nestedExpression() { return *const_cast(&m_matrix); } - - typedef TriangularView ConjugateReturnType; - /** \sa MatrixBase::conjugate() */ - EIGEN_DEVICE_FUNC - inline ConjugateReturnType conjugate() - { return ConjugateReturnType(m_matrix.conjugate()); } + /** \sa MatrixBase::conjugate() const */ - + typedef TriangularView ConjugateReturnType; EIGEN_DEVICE_FUNC inline const ConjugateReturnType conjugate() const { return ConjugateReturnType(m_matrix.conjugate()); } @@ -253,7 +249,8 @@ template class TriangularView inline TransposeReturnType transpose() { EIGEN_STATIC_ASSERT_LVALUE(MatrixType) - return TransposeReturnType(m_matrix.const_cast_derived().transpose()); + typename MatrixType::TransposeReturnType tmp(m_matrix.const_cast_derived()); + return TransposeReturnType(tmp); } typedef TriangularView ConstTransposeReturnType; @@ -392,6 +389,7 @@ template class TriangularViewImpl<_Mat EIGEN_DEVICE_FUNC inline Scalar& coeffRef(Index row, Index col) { + EIGEN_STATIC_ASSERT_LVALUE(TriangularViewType); Base::check_coordinates_internal(row, col); return derived().nestedExpression().const_cast_derived().coeffRef(row, col); } diff --git a/Eigen/src/SparseCholesky/SimplicialCholesky.h b/Eigen/src/SparseCholesky/SimplicialCholesky.h index 0e8fa6628..918a34e13 100644 --- a/Eigen/src/SparseCholesky/SimplicialCholesky.h +++ b/Eigen/src/SparseCholesky/SimplicialCholesky.h @@ -237,8 +237,8 @@ template struct traits CholMatrixType; - typedef TriangularView MatrixL; - typedef TriangularView MatrixU; + typedef TriangularView MatrixL; + typedef TriangularView MatrixU; static inline MatrixL getL(const MatrixType& m) { return MatrixL(m); } static inline MatrixU getU(const MatrixType& m) { return MatrixU(m.adjoint()); } }; @@ -251,8 +251,8 @@ template struct traits CholMatrixType; - typedef TriangularView MatrixL; - typedef TriangularView MatrixU; + typedef TriangularView MatrixL; + typedef TriangularView MatrixU; static inline MatrixL getL(const MatrixType& m) { return MatrixL(m); } static inline MatrixU getU(const MatrixType& m) { return MatrixU(m.adjoint()); } }; diff --git a/Eigen/src/SparseCore/SparseMatrixBase.h b/Eigen/src/SparseCore/SparseMatrixBase.h index f96042f27..04baabe4f 100644 --- a/Eigen/src/SparseCore/SparseMatrixBase.h +++ b/Eigen/src/SparseCore/SparseMatrixBase.h @@ -297,9 +297,9 @@ template class SparseMatrixBase : public EigenBase Derived& operator*=(const SparseMatrixBase& other); template - inline const TriangularView triangularView() const; + inline const TriangularView triangularView() const; - template inline const SparseSelfAdjointView selfadjointView() const; + template inline const SparseSelfAdjointView selfadjointView() const; template inline SparseSelfAdjointView selfadjointView(); template Scalar dot(const MatrixBase& other) const; diff --git a/Eigen/src/SparseCore/SparseSelfAdjointView.h b/Eigen/src/SparseCore/SparseSelfAdjointView.h index 247d4e6d3..5da7d2bef 100644 --- a/Eigen/src/SparseCore/SparseSelfAdjointView.h +++ b/Eigen/src/SparseCore/SparseSelfAdjointView.h @@ -170,9 +170,9 @@ template class SparseSelfAdjointView template template -const SparseSelfAdjointView SparseMatrixBase::selfadjointView() const +const SparseSelfAdjointView SparseMatrixBase::selfadjointView() const { - return SparseSelfAdjointView(derived()); + return SparseSelfAdjointView(derived()); } template diff --git a/Eigen/src/SparseCore/SparseTriangularView.h b/Eigen/src/SparseCore/SparseTriangularView.h index e200bc815..1f5e53155 100644 --- a/Eigen/src/SparseCore/SparseTriangularView.h +++ b/Eigen/src/SparseCore/SparseTriangularView.h @@ -266,10 +266,10 @@ protected: template template -inline const TriangularView +inline const TriangularView SparseMatrixBase::triangularView() const { - return TriangularView(derived()); + return TriangularView(derived()); } } // end namespace Eigen