From d58678069c6c90ac6fe6bdeac5e95fd9ea1a2235 Mon Sep 17 00:00:00 2001 From: Christoph Hertzberg Date: Fri, 9 Apr 2021 17:03:28 +0000 Subject: Make iterators default constructible and assignable, by making... --- Eigen/src/Core/StlIterators.h | 19 ++++++++++++------- Eigen/src/Core/util/XprHelper.h | 5 ++--- test/stl_iterators.cpp | 30 +++++++++++++++++++++++++++++- 3 files changed, 43 insertions(+), 11 deletions(-) diff --git a/Eigen/src/Core/StlIterators.h b/Eigen/src/Core/StlIterators.h index a2bc0dece..09041db1d 100644 --- a/Eigen/src/Core/StlIterators.h +++ b/Eigen/src/Core/StlIterators.h @@ -7,6 +7,9 @@ // Public License v. 2.0. If a copy of the MPL was not distributed // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. +#ifndef EIGEN_STLITERATORS_H +#define EIGEN_STLITERATORS_H + namespace Eigen { namespace internal { @@ -30,10 +33,10 @@ public: typedef Index difference_type; typedef std::random_access_iterator_tag iterator_category; - indexed_based_stl_iterator_base() : mp_xpr(0), m_index(0) {} - indexed_based_stl_iterator_base(XprType& xpr, Index index) : mp_xpr(&xpr), m_index(index) {} + indexed_based_stl_iterator_base() EIGEN_NO_THROW : mp_xpr(0), m_index(0) {} + indexed_based_stl_iterator_base(XprType& xpr, Index index) EIGEN_NO_THROW : mp_xpr(&xpr), m_index(index) {} - indexed_based_stl_iterator_base(const non_const_iterator& other) + indexed_based_stl_iterator_base(const non_const_iterator& other) EIGEN_NO_THROW : mp_xpr(other.mp_xpr), m_index(other.m_index) {} @@ -190,17 +193,17 @@ public: typedef typename internal::conditional::type reference; - pointer_based_stl_iterator() : m_ptr(0) {} - pointer_based_stl_iterator(XprType& xpr, Index index) : m_incr(xpr.innerStride()) + pointer_based_stl_iterator() EIGEN_NO_THROW : m_ptr(0) {} + pointer_based_stl_iterator(XprType& xpr, Index index) EIGEN_NO_THROW : m_incr(xpr.innerStride()) { m_ptr = xpr.data() + index * m_incr.value(); } - pointer_based_stl_iterator(const non_const_iterator& other) + pointer_based_stl_iterator(const non_const_iterator& other) EIGEN_NO_THROW : m_ptr(other.m_ptr), m_incr(other.m_incr) {} - pointer_based_stl_iterator& operator=(const non_const_iterator& other) + pointer_based_stl_iterator& operator=(const non_const_iterator& other) EIGEN_NO_THROW { m_ptr = other.m_ptr; m_incr.setValue(other.m_incr); @@ -456,3 +459,5 @@ inline typename DenseBase::const_iterator DenseBase::cend() co } } // namespace Eigen + +#endif // EIGEN_STLITERATORS_H diff --git a/Eigen/src/Core/util/XprHelper.h b/Eigen/src/Core/util/XprHelper.h index 9c7771797..2c63a9524 100644 --- a/Eigen/src/Core/util/XprHelper.h +++ b/Eigen/src/Core/util/XprHelper.h @@ -136,15 +136,14 @@ template class variable_if_dynamic EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR operator T() const { return T(Value); } EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE - void setValue(T) const {} + void setValue(T v) const { EIGEN_ONLY_USED_FOR_DEBUG(v); eigen_assert(v == T(Value)); } }; template class variable_if_dynamic { T m_value; - EIGEN_DEVICE_FUNC variable_if_dynamic() { eigen_assert(false); } public: - EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE explicit variable_if_dynamic(T value) : m_value(value) {} + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE explicit variable_if_dynamic(T value = 0) EIGEN_NO_THROW : m_value(value) {} EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T value() const { return m_value; } EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE operator T() const { return m_value; } EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void setValue(T value) { m_value = value; } diff --git a/test/stl_iterators.cpp b/test/stl_iterators.cpp index 997f8016c..72bbf8250 100644 --- a/test/stl_iterators.cpp +++ b/test/stl_iterators.cpp @@ -7,9 +7,9 @@ // Public License v. 2.0. If a copy of the MPL was not distributed // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. +#include "main.h" #include #include -#include "main.h" template< class Iterator > std::reverse_iterator @@ -47,6 +47,18 @@ bool is_pointer_based_stl_iterator(const internal::pointer_based_stl_iterator bool is_generic_randaccess_stl_iterator(const internal::generic_randaccess_stl_iterator &) { return true; } +template +bool is_default_constructible_and_assignable(const Iter& it) +{ +#if EIGEN_HAS_CXX11 + VERIFY(std::is_default_constructible::value); + VERIFY(std::is_nothrow_default_constructible::value); +#endif + Iter it2; + it2 = it; + return (it==it2); +} + template void check_begin_end_for_loop(Xpr xpr) { @@ -124,6 +136,22 @@ void test_stl_iterators(int rows=Rows, int cols=Cols) Index i, j; + // Verify that iterators are default constructible (See bug #1900) + { + VERIFY( is_default_constructible_and_assignable(v.begin())); + VERIFY( is_default_constructible_and_assignable(v.end())); + VERIFY( is_default_constructible_and_assignable(cv.begin())); + VERIFY( is_default_constructible_and_assignable(cv.end())); + + VERIFY( is_default_constructible_and_assignable(A.row(0).begin())); + VERIFY( is_default_constructible_and_assignable(A.row(0).end())); + VERIFY( is_default_constructible_and_assignable(cA.row(0).begin())); + VERIFY( is_default_constructible_and_assignable(cA.row(0).end())); + + VERIFY( is_default_constructible_and_assignable(B.row(0).begin())); + VERIFY( is_default_constructible_and_assignable(B.row(0).end())); + } + // Check we got a fast pointer-based iterator when expected { VERIFY( is_pointer_based_stl_iterator(v.begin()) ); -- cgit v1.2.3