Make iterators default constructible and assignable, by making...

This commit is contained in:
Christoph Hertzberg 2021-04-09 17:03:28 +00:00 committed by Rasmus Munk Larsen
parent 2859db0220
commit d58678069c
3 changed files with 43 additions and 11 deletions

View File

@ -7,6 +7,9 @@
// Public License v. 2.0. If a copy of the MPL was not distributed // 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/. // 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 Eigen {
namespace internal { namespace internal {
@ -30,10 +33,10 @@ public:
typedef Index difference_type; typedef Index difference_type;
typedef std::random_access_iterator_tag iterator_category; typedef std::random_access_iterator_tag iterator_category;
indexed_based_stl_iterator_base() : mp_xpr(0), m_index(0) {} indexed_based_stl_iterator_base() EIGEN_NO_THROW : 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(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) : mp_xpr(other.mp_xpr), m_index(other.m_index)
{} {}
@ -190,17 +193,17 @@ public:
typedef typename internal::conditional<bool(is_lvalue), value_type&, const value_type&>::type reference; typedef typename internal::conditional<bool(is_lvalue), value_type&, const value_type&>::type reference;
pointer_based_stl_iterator() : m_ptr(0) {} pointer_based_stl_iterator() EIGEN_NO_THROW : m_ptr(0) {}
pointer_based_stl_iterator(XprType& xpr, Index index) : m_incr(xpr.innerStride()) pointer_based_stl_iterator(XprType& xpr, Index index) EIGEN_NO_THROW : m_incr(xpr.innerStride())
{ {
m_ptr = xpr.data() + index * m_incr.value(); 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) : 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_ptr = other.m_ptr;
m_incr.setValue(other.m_incr); m_incr.setValue(other.m_incr);
@ -456,3 +459,5 @@ inline typename DenseBase<Derived>::const_iterator DenseBase<Derived>::cend() co
} }
} // namespace Eigen } // namespace Eigen
#endif // EIGEN_STLITERATORS_H

View File

@ -136,15 +136,14 @@ template<typename T, int Value> class variable_if_dynamic
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR
operator T() const { return T(Value); } operator T() const { return T(Value); }
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE 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<typename T> class variable_if_dynamic<T, Dynamic> template<typename T> class variable_if_dynamic<T, Dynamic>
{ {
T m_value; T m_value;
EIGEN_DEVICE_FUNC variable_if_dynamic() { eigen_assert(false); }
public: 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 T value() const { return m_value; }
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE operator T() 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; } EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void setValue(T value) { m_value = value; }

View File

@ -7,9 +7,9 @@
// Public License v. 2.0. If a copy of the MPL was not distributed // 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/. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include "main.h"
#include <iterator> #include <iterator>
#include <numeric> #include <numeric>
#include "main.h"
template< class Iterator > template< class Iterator >
std::reverse_iterator<Iterator> std::reverse_iterator<Iterator>
@ -47,6 +47,18 @@ bool is_pointer_based_stl_iterator(const internal::pointer_based_stl_iterator<Xp
template<typename XprType> template<typename XprType>
bool is_generic_randaccess_stl_iterator(const internal::generic_randaccess_stl_iterator<XprType> &) { return true; } bool is_generic_randaccess_stl_iterator(const internal::generic_randaccess_stl_iterator<XprType> &) { return true; }
template<typename Iter>
bool is_default_constructible_and_assignable(const Iter& it)
{
#if EIGEN_HAS_CXX11
VERIFY(std::is_default_constructible<Iter>::value);
VERIFY(std::is_nothrow_default_constructible<Iter>::value);
#endif
Iter it2;
it2 = it;
return (it==it2);
}
template<typename Xpr> template<typename Xpr>
void check_begin_end_for_loop(Xpr xpr) 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; 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 // Check we got a fast pointer-based iterator when expected
{ {
VERIFY( is_pointer_based_stl_iterator(v.begin()) ); VERIFY( is_pointer_based_stl_iterator(v.begin()) );