From c0ca8a9fa3e03ad7ecb270adfe760a1bff7c0829 Mon Sep 17 00:00:00 2001 From: Eugene Zhulenev Date: Tue, 9 Oct 2018 15:28:23 -0700 Subject: Compile time detection for unimplemented stl-style iterators --- test/stl_iterators.cpp | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'test/stl_iterators.cpp') 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() )); } -- cgit v1.2.3