[patch] C++14: N3671 Making non-modifying sequence operations more robust

François Dumont frs.dumont@gmail.com
Mon Jun 10 20:16:00 GMT 2013


On 06/08/2013 06:13 PM, Jonathan Wakely wrote:
> Another C++14 feature.
>
>          * include/bits/stl_algo.h (is_permutation): Add overloads from N3671.
>          * include/bits/stl_algobase.h (equal, mismatch): Likewise.
>          * testsuite/25_algorithms/equal/1.cc: Remove duplicate test case.
>          * testsuite/25_algorithms/equal/2.cc: New.
>          * testsuite/25_algorithms/equal/check_type2.cc: New.
>          * testsuite/25_algorithms/is_permutationqual/2.cc: New.
>          * testsuite/25_algorithms/is_permutationqual/check_type2.cc: New.
>          * testsuite/25_algorithms/mismatch/2.cc: New.
>          * testsuite/25_algorithms/mismatch/check_type2.cc: New.
>          * testsuite/util/testsuite_iterators.h: Fix spelling.
>
> Tested x86_64-linux, committed to trunk.

+
+#if __cplusplus > 201103L
+      template<typename _II1, typename _II2>
+        static bool
+        equal(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
+        {
+	  for (; __first1 != __last1 && __first2 != __last2;
+	      ++__first1, ++__first2)
+	    if (!(*__first1 == *__first2))
+	      return false;
+	  return true;
+	}
+#endif
      };
  
    template<>
@@ -810,6 +823,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
  	  return !__builtin_memcmp(__first1, __first2, sizeof(_Tp)
  				   * (__last1 - __first1));
  	}
+
+#if __cplusplus > 201103L
+      template<typename _Tp>
+        static bool
+        equal(const _Tp* __first1, const _Tp* __last1, const _Tp* __first2,
+	      const _Tp* __last2)
+        {
+	  return !__builtin_memcmp(__first1, __first2, sizeof(_Tp)
+				   * (__last1 - __first1));
+	}
+#endif
      };

  
I think there is a problem here. I had a closer look because __last2 is not consider in the version taking pointers. We could start reading after __last2, something Valgrin could catch perhaps. A consistent version would be:

+#if __cplusplus > 201103L
+      template<typename _Tp>
+        static bool
+        equal(const _Tp* __first1, const _Tp* __last1, const _Tp* __first2,
+	      const _Tp* __last2)
+        {
+	  if (__builtin_memcmp(__first1, __first2, sizeof(_Tp)
+			      * std::min(__last1 - __first1, __last2 - __first2))
+           return false;
+         return true;
+	}
+#endif

But I am not sure it is the intended behavior because I can't see where those equal overloads are used. It looks like __equal2 has been implemented and used instead. Maybe some dead code to clean, no ?

François



More information about the Libstdc++ mailing list