From c0ca8a9fa3e03ad7ecb270adfe760a1bff7c0829 Mon Sep 17 00:00:00 2001 From: Eugene Zhulenev Date: Tue, 9 Oct 2018 15:28:23 -0700 Subject: [PATCH] Compile time detection for unimplemented stl-style iterators --- Eigen/src/Core/DenseBase.h | 14 +++++- Eigen/src/Core/StlIterators.h | 13 ++++++ Eigen/src/Core/util/ForwardDeclarations.h | 1 + test/stl_iterators.cpp | 56 +++++++++++++++++++++++ 4 files changed, 83 insertions(+), 1 deletion(-) diff --git a/Eigen/src/Core/DenseBase.h b/Eigen/src/Core/DenseBase.h index 0c34bce40..eedeacc94 100644 --- a/Eigen/src/Core/DenseBase.h +++ b/Eigen/src/Core/DenseBase.h @@ -583,11 +583,23 @@ template class DenseBase typedef typename internal::conditional< (Flags&DirectAccessBit)==DirectAccessBit, internal::pointer_based_stl_iterator, internal::generic_randaccess_stl_iterator - >::type iterator; + >::type iterator_type; typedef typename internal::conditional< (Flags&DirectAccessBit)==DirectAccessBit, internal::pointer_based_stl_iterator, internal::generic_randaccess_stl_iterator + >::type const_iterator_type; + + // Stl-style iterators are supported only for vectors. + + typedef typename internal::conditional< IsVectorAtCompileTime, + iterator_type, + internal::not_an_iterator + >::type iterator; + + typedef typename internal::conditional< IsVectorAtCompileTime, + const_iterator_type, + internal::not_an_iterator >::type const_iterator; #endif diff --git a/Eigen/src/Core/StlIterators.h b/Eigen/src/Core/StlIterators.h index 24eef1269..5f2edc4e6 100644 --- a/Eigen/src/Core/StlIterators.h +++ b/Eigen/src/Core/StlIterators.h @@ -11,6 +11,19 @@ namespace Eigen { namespace internal { +// Iterator type for XprType that do not support stl-style iterators. Allows to +// detect that expression does not support stl iterators at compile time. +template +class not_an_iterator +{ + not_an_iterator() : mp_xpr(0), m_index(0) {} + not_an_iterator(XprType& xpr, Index index) : mp_xpr(&xpr), m_index(index) {} + + protected: + XprType *mp_xpr; + Index m_index; +}; + template class indexed_based_stl_iterator_base { diff --git a/Eigen/src/Core/util/ForwardDeclarations.h b/Eigen/src/Core/util/ForwardDeclarations.h index 3ab3a5f50..49c4e0c31 100644 --- a/Eigen/src/Core/util/ForwardDeclarations.h +++ b/Eigen/src/Core/util/ForwardDeclarations.h @@ -135,6 +135,7 @@ template class SolverBase; template class InnerIterator; namespace internal { +template class not_an_iterator; template class generic_randaccess_stl_iterator; template class pointer_based_stl_iterator; template class subvector_stl_iterator; diff --git a/test/stl_iterators.cpp b/test/stl_iterators.cpp index a9fe3afc2..75e23b2fb 100644 --- a/test/stl_iterators.cpp +++ b/test/stl_iterators.cpp @@ -358,6 +358,59 @@ void test_stl_iterators(int rows=Rows, int cols=Cols) #endif } + +#if EIGEN_HAS_CXX11 +// When the compiler sees expression IsContainerTest(0), if C is an +// STL-style container class, the first overload of IsContainerTest +// will be viable (since both C::iterator* and C::const_iterator* are +// valid types and NULL can be implicitly converted to them). It will +// be picked over the second overload as 'int' is a perfect match for +// the type of argument 0. If C::iterator or C::const_iterator is not +// a valid type, the first overload is not viable, and the second +// overload will be picked. +template ().begin()), + class = decltype(::std::declval().end()), + class = decltype(++::std::declval()), + class = decltype(*::std::declval()), + class = typename C::const_iterator> +bool IsContainerType(int /* dummy */) { return true; } + +template +bool IsContainerType(long /* dummy */) { return false; } +#endif // EIGEN_HAS_CXX11 + +template +void test_stl_container_detection(int rows=Rows, int cols=Cols) +{ +#if EIGEN_HAS_CXX11 + typedef Matrix VectorType; + typedef Matrix ColMatrixType; + typedef Matrix RowMatrixType; + + ColMatrixType A = ColMatrixType::Random(rows, cols); + RowMatrixType B = RowMatrixType::Random(rows, cols); + + Index i; + + using ColMatrixColType = decltype(A.col(i)); + using ColMatrixRowType = decltype(A.row(i)); + using RowMatrixColType = decltype(B.col(i)); + using RowMatrixRowType = decltype(B.row(i)); + + // Vector and matrix col/row are valid Stl-style container. + VERIFY_IS_EQUAL(IsContainerType(0), true); + VERIFY_IS_EQUAL(IsContainerType(0), true); + VERIFY_IS_EQUAL(IsContainerType(0), true); + VERIFY_IS_EQUAL(IsContainerType(0), true); + VERIFY_IS_EQUAL(IsContainerType(0), true); + + // But the matrix itself is not a valid Stl-style container. + VERIFY_IS_EQUAL(IsContainerType(0), rows == 1 || cols == 1); + VERIFY_IS_EQUAL(IsContainerType(0), rows == 1 || cols == 1); +#endif +} + EIGEN_DECLARE_TEST(stl_iterators) { for(int i = 0; i < g_repeat; i++) { @@ -366,4 +419,7 @@ EIGEN_DECLARE_TEST(stl_iterators) CALL_SUBTEST_1(( test_stl_iterators(internal::random(5,10), internal::random(5,10)) )); CALL_SUBTEST_1(( test_stl_iterators(internal::random(10,200), internal::random(10,200)) )); } + + CALL_SUBTEST_1(( test_stl_container_detection() )); + CALL_SUBTEST_1(( test_stl_container_detection() )); }