diff --git a/Eigen/src/Core/CoreEvaluators.h b/Eigen/src/Core/CoreEvaluators.h index 879b0dbd5..156ca2b60 100644 --- a/Eigen/src/Core/CoreEvaluators.h +++ b/Eigen/src/Core/CoreEvaluators.h @@ -124,8 +124,7 @@ struct evaluator_base { // noncopyable: // Don't make this class inherit noncopyable as this kills EBO (Empty Base Optimization) // and make complex evaluator much larger than then should do. - EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE evaluator_base() {} - EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE ~evaluator_base() {} + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr evaluator_base() = default; private: EIGEN_DEVICE_FUNC evaluator_base(const evaluator_base&); @@ -143,7 +142,7 @@ struct evaluator_base { template class plainobjectbase_evaluator_data { public: - EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE plainobjectbase_evaluator_data(const Scalar* ptr, Index outerStride) + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr plainobjectbase_evaluator_data(const Scalar* ptr, Index outerStride) : data(ptr) { #ifndef EIGEN_INTERNAL_DEBUGGING EIGEN_UNUSED_VARIABLE(outerStride); @@ -157,9 +156,9 @@ class plainobjectbase_evaluator_data { template class plainobjectbase_evaluator_data { public: - EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE plainobjectbase_evaluator_data(const Scalar* ptr, Index outerStride) + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr plainobjectbase_evaluator_data(const Scalar* ptr, Index outerStride) : data(ptr), m_outerStride(outerStride) {} - EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index outerStride() const { return m_outerStride; } + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr Index outerStride() const { return m_outerStride; } const Scalar* data; protected: @@ -189,32 +188,34 @@ struct evaluator> : evaluator_base { : RowsAtCompileTime }; - EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE evaluator() : m_d(0, OuterStrideAtCompileTime) { + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr evaluator() : m_d(0, OuterStrideAtCompileTime) { EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost); } - EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE explicit evaluator(const PlainObjectType& m) + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr explicit evaluator(const PlainObjectType& m) : m_d(m.data(), IsVectorAtCompileTime ? 0 : m.outerStride()) { EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost); } - EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index row, Index col) const { + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr CoeffReturnType coeff(Index row, Index col) const { if (IsRowMajor) return m_d.data[row * m_d.outerStride() + col]; else return m_d.data[row + col * m_d.outerStride()]; } - EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const { return m_d.data[index]; } + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr CoeffReturnType coeff(Index index) const { return m_d.data[index]; } - EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(Index row, Index col) { + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr Scalar& coeffRef(Index row, Index col) { if (IsRowMajor) return const_cast(m_d.data)[row * m_d.outerStride() + col]; else return const_cast(m_d.data)[row + col * m_d.outerStride()]; } - EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(Index index) { return const_cast(m_d.data)[index]; } + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr Scalar& coeffRef(Index index) { + return const_cast(m_d.data)[index]; + } template EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketType packet(Index row, Index col) const { @@ -251,9 +252,10 @@ struct evaluator> : evaluator>> { typedef Matrix XprType; - EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE evaluator() {} + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr evaluator() = default; - EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE explicit evaluator(const XprType& m) : evaluator>(m) {} + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr explicit evaluator(const XprType& m) + : evaluator>(m) {} }; template @@ -261,9 +263,10 @@ struct evaluator> : evaluator>> { typedef Array XprType; - EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE evaluator() {} + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr evaluator() = default; - EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE explicit evaluator(const XprType& m) : evaluator>(m) {} + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr explicit evaluator(const XprType& m) + : evaluator>(m) {} }; // -------------------- Transpose -------------------- diff --git a/Eigen/src/Core/DenseCoeffsBase.h b/Eigen/src/Core/DenseCoeffsBase.h index 30e0aa38a..97f9b50f3 100644 --- a/Eigen/src/Core/DenseCoeffsBase.h +++ b/Eigen/src/Core/DenseCoeffsBase.h @@ -298,7 +298,7 @@ class DenseCoeffsBase : public DenseCoeffsBase= 0 && row < rows() && col >= 0 && col < cols()); return internal::evaluator(derived()).coeffRef(row, col); } @@ -312,7 +312,7 @@ class DenseCoeffsBase : public DenseCoeffsBase= 0 && row < rows() && col >= 0 && col < cols()); return coeffRef(row, col); } @@ -332,7 +332,7 @@ class DenseCoeffsBase : public DenseCoeffsBase::Flags & LinearAccessBit, THIS_COEFFICIENT_ACCESSOR_TAKING_ONE_ACCESS_IS_ONLY_FOR_EXPRESSIONS_ALLOWING_LINEAR_ACCESS) eigen_internal_assert(index >= 0 && index < size()); @@ -346,7 +346,7 @@ class DenseCoeffsBase : public DenseCoeffsBase= 0 && index < size()); diff --git a/Eigen/src/Core/EigenBase.h b/Eigen/src/Core/EigenBase.h index a2d6ee2ec..6d167006a 100644 --- a/Eigen/src/Core/EigenBase.h +++ b/Eigen/src/Core/EigenBase.h @@ -46,9 +46,9 @@ struct EigenBase { typedef typename internal::traits::StorageKind StorageKind; /** \returns a reference to the derived object */ - EIGEN_DEVICE_FUNC Derived& derived() { return *static_cast(this); } + EIGEN_DEVICE_FUNC constexpr Derived& derived() { return *static_cast(this); } /** \returns a const reference to the derived object */ - EIGEN_DEVICE_FUNC const Derived& derived() const { return *static_cast(this); } + EIGEN_DEVICE_FUNC constexpr const Derived& derived() const { return *static_cast(this); } EIGEN_DEVICE_FUNC inline Derived& const_cast_derived() const { return *static_cast(const_cast(this)); diff --git a/test/constexpr.cpp b/test/constexpr.cpp index 9fdf4478a..34c728f66 100644 --- a/test/constexpr.cpp +++ b/test/constexpr.cpp @@ -16,38 +16,48 @@ EIGEN_DECLARE_TEST(constexpr) { // until after the constructor returns: // error: member ‘Eigen::internal::plain_array::array’ must be initialized by mem-initializer in // ‘constexpr’ constructor -#if EIGEN_COMP_CXXVER >= 20 +#if __cpp_constexpr >= 201907L constexpr Matrix3i mat({{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}); VERIFY_IS_EQUAL(mat.size(), 9); - VERIFY_IS_EQUAL(mat(0, 0), 1); + static_assert(mat(0, 0) == 1); + static_assert(mat(0) == 1); static_assert(mat.coeff(0, 1) == 2); constexpr Array33i arr({{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}); - VERIFY_IS_EQUAL(arr(0, 0), 1); + static_assert(arr(0, 0) == 1); + static_assert(arr(0) == 1); VERIFY_IS_EQUAL(arr.size(), 9); static_assert(arr.coeff(0, 1) == 2); + constexpr RowVector3i vec{{1, 2, 3}}; + static_assert(vec(0, 0) == 1); + static_assert(vec[0] == 1); + VERIFY_IS_EQUAL(vec.size(), 3); + static_assert(vec.coeff(0, 1) == 2); + // Also check dynamic size arrays/matrices with fixed-size storage (currently // only works if all elements are initialized, since otherwise the compiler // complains about uninitialized trailing elements. constexpr Matrix dyn_mat({{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}); VERIFY_IS_EQUAL(dyn_mat.size(), 9); - VERIFY_IS_EQUAL(dyn_mat(0, 0), 1); + static_assert(dyn_mat(0, 0) == 1); static_assert(dyn_mat.coeff(0, 1) == 2); constexpr Array dyn_arr({{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}); - VERIFY_IS_EQUAL(dyn_arr(0, 0), 1); + static_assert(dyn_arr(0, 0) == 1); + static_assert(dyn_arr(0) == 1); VERIFY_IS_EQUAL(dyn_arr.size(), 9); static_assert(dyn_arr.coeff(0, 1) == 2); -#endif // EIGEN_COMP_CXXVER >= 20 +#endif // __cpp_constexpr >= 201907L } // Check that we can use the std::initializer_list constructor for constexpr variables. -#if EIGEN_COMP_CXXVER >= 20 +#if __cpp_constexpr >= 201907L // EIGEN_MAKE_UNALIGNED_ARRAY_ASSERT() will fail constexpr evaluation unless // we have std::is_constant_evaluated(). constexpr Matrix global_mat({{1, 2}, {3, 4}}); EIGEN_DECLARE_TEST(constexpr_global) { VERIFY_IS_EQUAL(global_mat.size(), 4); - VERIFY_IS_EQUAL(global_mat(0, 0), 1); + static_assert(global_mat(0, 0) == 1); + static_assert(global_mat(0) == 1); static_assert(global_mat.coeff(0, 0) == 1); } -#endif // EIGEN_COMP_CXXVER >= 20 +#endif // __cpp_constexpr >= 201907L