[libcxx-commits] [libcxx] r364545 - Provide hashers for string_view only if they are using the default char_traits. Seen on SO: test/std/strings/string.view/string.view.hash/char_type.hash.fail.cpp

Marshall Clow via libcxx-commits libcxx-commits at lists.llvm.org
Thu Jun 27 07:18:33 PDT 2019


Author: marshall
Date: Thu Jun 27 07:18:32 2019
New Revision: 364545

URL: http://llvm.org/viewvc/llvm-project?rev=364545&view=rev
Log:
Provide hashers for string_view only if they are using the default char_traits. Seen on SO: test/std/strings/string.view/string.view.hash/char_type.hash.fail.cpp

Added:
    libcxx/trunk/test/std/strings/string.view/string.view.hash/char_type.hash.fail.cpp
Modified:
    libcxx/trunk/include/string_view
    libcxx/trunk/test/std/strings/basic.string.hash/char_type_hash.fail.cpp

Modified: libcxx/trunk/include/string_view
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/string_view?rev=364545&r1=364544&r2=364545&view=diff
==============================================================================
--- libcxx/trunk/include/string_view (original)
+++ libcxx/trunk/include/string_view Thu Jun 27 07:18:32 2019
@@ -776,12 +776,12 @@ typedef basic_string_view<char32_t> u32s
 typedef basic_string_view<wchar_t>  wstring_view;
 
 // [string.view.hash]
-template<class _CharT, class _Traits>
-struct _LIBCPP_TEMPLATE_VIS hash<basic_string_view<_CharT, _Traits> >
-    : public unary_function<basic_string_view<_CharT, _Traits>, size_t>
+template<class _CharT>
+struct _LIBCPP_TEMPLATE_VIS hash<basic_string_view<_CharT, char_traits<_CharT> > >
+    : public unary_function<basic_string_view<_CharT, char_traits<_CharT> >, size_t>
 {
     _LIBCPP_INLINE_VISIBILITY
-    size_t operator()(const basic_string_view<_CharT, _Traits> __val) const _NOEXCEPT {
+    size_t operator()(const basic_string_view<_CharT, char_traits<_CharT> > __val) const _NOEXCEPT {
         return __do_string_hash(__val.data(), __val.data() + __val.size());
     }
 };

Modified: libcxx/trunk/test/std/strings/basic.string.hash/char_type_hash.fail.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/strings/basic.string.hash/char_type_hash.fail.cpp?rev=364545&r1=364544&r2=364545&view=diff
==============================================================================
--- libcxx/trunk/test/std/strings/basic.string.hash/char_type_hash.fail.cpp (original)
+++ libcxx/trunk/test/std/strings/basic.string.hash/char_type_hash.fail.cpp Thu Jun 27 07:18:32 2019
@@ -53,7 +53,7 @@ template <class CharT>
 void test() {
     typedef std::basic_string<CharT, trait<CharT> > str_t;
     std::hash<str_t>
-        h; // call to implicitly-deleted default constructor of 'std::hash<str_t>' {{*}}}}
+        h; // expected-error-re 4 {{call to implicitly-deleted default constructor of 'std::hash<str_t>' {{.+}}}}}
 }
 
 int main(int, char**) {

Added: libcxx/trunk/test/std/strings/string.view/string.view.hash/char_type.hash.fail.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/strings/string.view/string.view.hash/char_type.hash.fail.cpp?rev=364545&view=auto
==============================================================================
--- libcxx/trunk/test/std/strings/string.view/string.view.hash/char_type.hash.fail.cpp (added)
+++ libcxx/trunk/test/std/strings/string.view/string.view.hash/char_type.hash.fail.cpp Thu Jun 27 07:18:32 2019
@@ -0,0 +1,67 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++98, c++03
+
+// <string>
+
+// Test that hash specializations for <string_view> require "char_traits<_CharT>" not just any "_Trait".
+
+#include <string_view>
+#include <string> // for 'mbstate_t'
+
+template <class _CharT>
+struct trait // copied from <__string>
+{
+    typedef _CharT         char_type;
+    typedef int            int_type;
+    typedef std::streamoff off_type;
+    typedef std::streampos pos_type;
+    typedef std::mbstate_t state_type;
+
+    static inline void assign(char_type& __c1, const char_type& __c2) {
+        __c1 = __c2;
+    }
+    static inline bool eq(char_type __c1, char_type __c2) { return __c1 == __c2; }
+    static inline bool lt(char_type __c1, char_type __c2) { return __c1 < __c2; }
+
+    static int compare(const char_type* __s1, const char_type* __s2, size_t __n);
+    static size_t length(const char_type* __s);
+    static const char_type* find(const char_type* __s, size_t __n,
+                                 const char_type& __a);
+
+    static char_type* move(char_type* __s1, const char_type* __s2, size_t __n);
+    static char_type* copy(char_type* __s1, const char_type* __s2, size_t __n);
+    static char_type* assign(char_type* __s, size_t __n, char_type __a);
+
+    static inline int_type not_eof(int_type __c) {
+        return eq_int_type(__c, eof()) ? ~eof() : __c;
+    }
+    static inline char_type to_char_type(int_type __c) { return char_type(__c); }
+    static inline int_type to_int_type(char_type __c) { return int_type(__c); }
+    static inline bool eq_int_type(int_type __c1, int_type __c2) {
+        return __c1 == __c2;
+    }
+    static inline int_type eof() { return int_type(EOF); }
+};
+
+template <class CharT>
+void test() {
+    typedef std::basic_string_view<CharT, trait<CharT> > strv_t;
+    std::hash<strv_t>
+        h; // expected-error-re 4 {{call to implicitly-deleted default constructor of 'std::hash<strv_t>' {{.+}}}}}}
+}
+
+int main(int, char**) {
+    test<char>();
+    test<wchar_t>();
+    test<char16_t>();
+    test<char32_t>();
+
+    return 0;
+}




More information about the libcxx-commits mailing list