From cda19a6255d25c3b834d142d74490b720425c49b Mon Sep 17 00:00:00 2001 From: AnonymousPC Date: Sat, 7 Jun 2025 03:15:18 +0800 Subject: [PATCH] Make `Eigen::Map::operator[]` return correct type --- Eigen/src/Core/DenseCoeffsBase.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Eigen/src/Core/DenseCoeffsBase.h b/Eigen/src/Core/DenseCoeffsBase.h index cff104c36..377df574f 100644 --- a/Eigen/src/Core/DenseCoeffsBase.h +++ b/Eigen/src/Core/DenseCoeffsBase.h @@ -45,10 +45,16 @@ class DenseCoeffsBase : public EigenBase { // - This is the return type of the coeff() method. // - The LvalueBit means exactly that we can offer a coeffRef() method, which means exactly that we can get references // to coeffs, which means exactly that we can have coeff() return a const reference (as opposed to returning a value). + // - The DirectAccessBit means exactly that the underlying data of coefficients can be directly accessed as a plain + // strided array, which means exactly that the underlying data of coefficients does exist in memory, which means + // exactly that the coefficients is const-referencable, which means exactly that we can have coeff() return a const + // reference. For example, Map have DirectAccessBit but not LvalueBit, so that Map.coeff() + // does points to a const Scalar& which exists in memory, while does not allow coeffRef() as it would not provide a + // lvalue. Notice that DirectAccessBit and LvalueBit are mutually orthogonal. // - The is_arithmetic check is required since "const int", "const double", etc. will cause warnings on some systems // while the declaration of "const T", where T is a non arithmetic type does not. Always returning "const Scalar&" is // not possible, since the underlying expressions might not offer a valid address the reference could be referring to. - typedef std::conditional_t::Flags& LvalueBit), const Scalar&, + typedef std::conditional_t::Flags&(LvalueBit | DirectAccessBit)), const Scalar&, std::conditional_t::value, Scalar, const Scalar>> CoeffReturnType;