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
This commit is contained in:
Tyler Veness
2025-11-04 07:03:04 +00:00
committed by Rasmus Munk Larsen
parent b6fcddccfc
commit f95b4698fc
6 changed files with 47 additions and 1 deletions

View File

@@ -113,6 +113,16 @@ class DenseCoeffsBase<Derived, ReadOnlyAccessors> : public EigenBase<Derived> {
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<Derived, WriteAccessors> : public DenseCoeffsBase<Derived,
*
* \sa operator[](Index)
*/
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr Scalar& operator()(Index row, Index col) {
eigen_assert(row >= 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.
*

View File

@@ -89,6 +89,11 @@ class TriangularBase : public EigenBase<Derived> {
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<const Derived*>(this); }
EIGEN_DEVICE_FUNC inline Derived& derived() { return *static_cast<Derived*>(this); }

View File

@@ -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 */