From 931843023707c0927279e0ad2533dd452b42806c Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Thu, 27 Jun 2019 14:18:32 +0000 Subject: [PATCH] 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 llvm-svn: 364545 --- libcxx/include/string_view | 8 +-- .../basic.string.hash/char_type_hash.fail.cpp | 2 +- .../string.view.hash/char_type.hash.fail.cpp | 67 +++++++++++++++++++ 3 files changed, 72 insertions(+), 5 deletions(-) create mode 100644 libcxx/test/std/strings/string.view/string.view.hash/char_type.hash.fail.cpp diff --git a/libcxx/include/string_view b/libcxx/include/string_view index aa93e1f6fa71..04448312ff33 100644 --- a/libcxx/include/string_view +++ b/libcxx/include/string_view @@ -776,12 +776,12 @@ typedef basic_string_view u32string_view; typedef basic_string_view wstring_view; // [string.view.hash] -template -struct _LIBCPP_TEMPLATE_VIS hash > - : public unary_function, size_t> +template +struct _LIBCPP_TEMPLATE_VIS hash > > + : public unary_function >, 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()); } }; diff --git a/libcxx/test/std/strings/basic.string.hash/char_type_hash.fail.cpp b/libcxx/test/std/strings/basic.string.hash/char_type_hash.fail.cpp index 542ae05536ea..7857ac7e3638 100644 --- a/libcxx/test/std/strings/basic.string.hash/char_type_hash.fail.cpp +++ b/libcxx/test/std/strings/basic.string.hash/char_type_hash.fail.cpp @@ -53,7 +53,7 @@ template void test() { typedef std::basic_string > str_t; std::hash - h; // call to implicitly-deleted default constructor of 'std::hash' {{*}}}} + h; // expected-error-re 4 {{call to implicitly-deleted default constructor of 'std::hash' {{.+}}}}} } int main(int, char**) { diff --git a/libcxx/test/std/strings/string.view/string.view.hash/char_type.hash.fail.cpp b/libcxx/test/std/strings/string.view/string.view.hash/char_type.hash.fail.cpp new file mode 100644 index 000000000000..587c989a8ed5 --- /dev/null +++ b/libcxx/test/std/strings/string.view/string.view.hash/char_type.hash.fail.cpp @@ -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 + +// + +// Test that hash specializations for require "char_traits<_CharT>" not just any "_Trait". + +#include +#include // for 'mbstate_t' + +template +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 +void test() { + typedef std::basic_string_view > strv_t; + std::hash + h; // expected-error-re 4 {{call to implicitly-deleted default constructor of 'std::hash' {{.+}}}}}} +} + +int main(int, char**) { + test(); + test(); + test(); + test(); + + return 0; +}