I've just committed this, and then noticed that we don't do the same
optimization for basic_string unless the char_type is char. Presumably
this is so that we do call basic_string::compare() and so call any
user-defined traits_type::eq() function (which is observable). I don't
think that's necessary, because the standard says operator== on
strings returns the result of lhs.compare(rhs) == 0, not that it's
"Equivalent to" such a call. As long as we give the right answer, I
don't think we need to make an explicit call to
basic_string::compare(). So maybe we want this for basic_string too.

        PR libstdc++/79206
        * include/experimental/string_view (operator==): Check sizes first.
        * include/std/string_view (operator==): Likewise.

Tested powerpc64le-linux, committed to trunk.

commit 131743b521289b143ef1e85a309f43d742e55481
Author: Jonathan Wakely <jwak...@redhat.com>
Date:   Tue Jan 24 11:48:56 2017 +0000

    PR libstdc++/79206 check string_view sizes in operator==
    
        PR libstdc++/79206
        * include/experimental/string_view (operator==): Check sizes first.
        * include/std/string_view (operator==): Likewise.

diff --git a/libstdc++-v3/include/experimental/string_view 
b/libstdc++-v3/include/experimental/string_view
index eaff0cc..2a2364c 100644
--- a/libstdc++-v3/include/experimental/string_view
+++ b/libstdc++-v3/include/experimental/string_view
@@ -456,19 +456,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     inline bool
     operator==(basic_string_view<_CharT, _Traits> __x,
                basic_string_view<_CharT, _Traits> __y) noexcept
-    { return __x.compare(__y) == 0; }
+    { return __x.size() == __y.size() && __x.compare(__y) == 0; }
 
   template<typename _CharT, typename _Traits>
     inline bool
     operator==(basic_string_view<_CharT, _Traits> __x,
                __detail::__idt<basic_string_view<_CharT, _Traits>> __y) 
noexcept
-    { return __x.compare(__y) == 0; }
+    { return __x.size() == __y.size() && __x.compare(__y) == 0; }
 
   template<typename _CharT, typename _Traits>
     inline bool
     operator==(__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
                basic_string_view<_CharT, _Traits> __y) noexcept
-    { return __x.compare(__y) == 0; }
+    { return __x.size() == __y.size() && __x.compare(__y) == 0; }
 
   template<typename _CharT, typename _Traits>
     inline bool
diff --git a/libstdc++-v3/include/std/string_view 
b/libstdc++-v3/include/std/string_view
index 9eee528..a719185 100644
--- a/libstdc++-v3/include/std/string_view
+++ b/libstdc++-v3/include/std/string_view
@@ -453,19 +453,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     inline bool
     operator==(basic_string_view<_CharT, _Traits> __x,
                basic_string_view<_CharT, _Traits> __y) noexcept
-    { return __x.compare(__y) == 0; }
+    { return __x.size() == __y.size() && __x.compare(__y) == 0; }
 
   template<typename _CharT, typename _Traits>
     inline bool
     operator==(basic_string_view<_CharT, _Traits> __x,
                __detail::__idt<basic_string_view<_CharT, _Traits>> __y) 
noexcept
-    { return __x.compare(__y) == 0; }
+    { return __x.size() == __y.size() && __x.compare(__y) == 0; }
 
   template<typename _CharT, typename _Traits>
     inline bool
     operator==(__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
                basic_string_view<_CharT, _Traits> __y) noexcept
-    { return __x.compare(__y) == 0; }
+    { return __x.size() == __y.size() && __x.compare(__y) == 0; }
 
   template<typename _CharT, typename _Traits>
     inline bool

Reply via email to