From f95b4698fceeac7ca6d74faa77af9f2f1f6b3425 Mon Sep 17 00:00:00 2001 From: Tyler Veness Date: Tue, 4 Nov 2025 07:03:04 +0000 Subject: [PATCH] Add support for C++23 multidimensional subscript operator I'm not sure where to put tests for this, assuming they're needed. They also wouldn't run in CI anyway since CI only exercises the C++17 codepaths. See merge request libeigen/eigen!2053 --- Eigen/src/Core/DenseCoeffsBase.h | 21 ++++++++++++++++++- Eigen/src/Core/TriangularMatrix.h | 5 +++++ Eigen/src/Geometry/Transform.h | 9 ++++++++ .../src/Tensor/TensorContractionMapper.h | 4 ++++ unsupported/Eigen/src/Polynomials/Companion.h | 4 ++++ .../Eigen/src/SparseExtra/RandomSetter.h | 5 +++++ 6 files changed, 47 insertions(+), 1 deletion(-) diff --git a/Eigen/src/Core/DenseCoeffsBase.h b/Eigen/src/Core/DenseCoeffsBase.h index 377df574f..86683fe31 100644 --- a/Eigen/src/Core/DenseCoeffsBase.h +++ b/Eigen/src/Core/DenseCoeffsBase.h @@ -113,6 +113,16 @@ class DenseCoeffsBase : public EigenBase { return coeff(row, col); } +#if __cpp_multidimensional_subscript >= 202110L + /** \returns the coefficient at given the given row and column. + * + * \sa operator[](Index,Index), operator[](Index) + */ + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr CoeffReturnType operator[](Index row, Index col) const { + return operator()(row, col); + } +#endif + /** Short version: don't use this function, use * \link operator[](Index) const \endlink instead. * @@ -316,12 +326,21 @@ class DenseCoeffsBase : public DenseCoeffsBase= 0 && row < rows() && col >= 0 && col < cols()); return coeffRef(row, col); } +#if __cpp_multidimensional_subscript >= 202110L + /** \returns a reference to the coefficient at given the given row and column. + * + * \sa operator[](Index) + */ + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr Scalar& operator[](Index row, Index col) { + return operator()(row, col); + } +#endif + /** Short version: don't use this function, use * \link operator[](Index) \endlink instead. * diff --git a/Eigen/src/Core/TriangularMatrix.h b/Eigen/src/Core/TriangularMatrix.h index 27ad78eca..910b5042c 100644 --- a/Eigen/src/Core/TriangularMatrix.h +++ b/Eigen/src/Core/TriangularMatrix.h @@ -89,6 +89,11 @@ class TriangularBase : public EigenBase { return coeffRef(row, col); } +#if __cpp_multidimensional_subscript >= 202110L + EIGEN_DEVICE_FUNC inline Scalar operator[](Index row, Index col) const { return operator()(row, col); } + EIGEN_DEVICE_FUNC inline Scalar& operator[](Index row, Index col) { return operator()(row, col); } +#endif + #ifndef EIGEN_PARSED_BY_DOXYGEN EIGEN_DEVICE_FUNC inline const Derived& derived() const { return *static_cast(this); } EIGEN_DEVICE_FUNC inline Derived& derived() { return *static_cast(this); } diff --git a/Eigen/src/Geometry/Transform.h b/Eigen/src/Geometry/Transform.h index a5d7b6083..f767c8b4f 100644 --- a/Eigen/src/Geometry/Transform.h +++ b/Eigen/src/Geometry/Transform.h @@ -365,6 +365,15 @@ class Transform { * \sa MatrixBase::operator(Index,Index) */ EIGEN_DEVICE_FUNC inline Scalar& operator()(Index row, Index col) { return m_matrix(row, col); } +#if __cpp_multidimensional_subscript >= 202110L + /** shortcut for m_matrix(row,col); + * \sa MatrixBase::operator(Index,Index) const */ + EIGEN_DEVICE_FUNC inline Scalar operator[](Index row, Index col) const { return m_matrix[row, col]; } + /** shortcut for m_matrix(row,col); + * \sa MatrixBase::operator(Index,Index) */ + EIGEN_DEVICE_FUNC inline Scalar& operator[](Index row, Index col) { return m_matrix[row, col]; } +#endif + /** \returns a read-only expression of the transformation matrix */ EIGEN_DEVICE_FUNC inline const MatrixType& matrix() const { return m_matrix; } /** \returns a writable expression of the transformation matrix */ diff --git a/unsupported/Eigen/CXX11/src/Tensor/TensorContractionMapper.h b/unsupported/Eigen/CXX11/src/Tensor/TensorContractionMapper.h index 6367db964..7b672fa6e 100644 --- a/unsupported/Eigen/CXX11/src/Tensor/TensorContractionMapper.h +++ b/unsupported/Eigen/CXX11/src/Tensor/TensorContractionMapper.h @@ -117,6 +117,10 @@ class SimpleTensorContractionMapper { return m_tensor.coeff(computeIndex(row, col)); } +#if __cpp_multidimensional_subscript >= 202110L + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar operator[](Index row, Index col) const { return operator()(row, col); } +#endif + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index computeIndex(Index row, Index col) const { const bool left = (side == Lhs); EIGEN_UNUSED_VARIABLE(left); // annoying bug in g++8.1: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85963 diff --git a/unsupported/Eigen/src/Polynomials/Companion.h b/unsupported/Eigen/src/Polynomials/Companion.h index f08099668..ea245a991 100644 --- a/unsupported/Eigen/src/Polynomials/Companion.h +++ b/unsupported/Eigen/src/Polynomials/Companion.h @@ -63,6 +63,10 @@ class companion { } } +#if __cpp_multidimensional_subscript >= 202110L + EIGEN_STRONG_INLINE const Scalar_ operator[](Index row, Index col) const { return operator()(row, col); } +#endif + public: template void setPolynomial(const VectorType& poly) { diff --git a/unsupported/Eigen/src/SparseExtra/RandomSetter.h b/unsupported/Eigen/src/SparseExtra/RandomSetter.h index 2889d82ab..2ba652304 100644 --- a/unsupported/Eigen/src/SparseExtra/RandomSetter.h +++ b/unsupported/Eigen/src/SparseExtra/RandomSetter.h @@ -280,6 +280,11 @@ class RandomSetter { return m_hashmaps[outerMajor][key].value; } +#if __cpp_multidimensional_subscript >= 202110L + /** \returns a reference to the coefficient at given coordinates \a row, \a col */ + Scalar& operator[](Index row, Index col) { return operator()(row, col); } +#endif + /** \returns the number of non zero coefficients * * \note According to the underlying map/hash_map implementation,