1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 // UNSUPPORTED: c++03
10 // UNSUPPORTED: !stdlib=libc++ && (c++11 || c++14)
11
12 // <string_view>
13
14 // Test that hash specializations for <string_view> require "char_traits<_CharT>" not just any "_Trait".
15
16 #include <string_view>
17 #include <string> // for 'mbstate_t'
18
19 #include "test_macros.h"
20
21 template <class _CharT>
22 struct trait // copied from <__string>
23 {
24 typedef _CharT char_type;
25 typedef int int_type;
26 typedef std::streamoff off_type;
27 typedef std::streampos pos_type;
28 typedef std::mbstate_t state_type;
29
assigntrait30 static inline void assign(char_type& __c1, const char_type& __c2) {
31 __c1 = __c2;
32 }
eqtrait33 static inline bool eq(char_type __c1, char_type __c2) { return __c1 == __c2; }
lttrait34 static inline bool lt(char_type __c1, char_type __c2) { return __c1 < __c2; }
35
36 static int compare(const char_type* __s1, const char_type* __s2, size_t __n);
37 static size_t length(const char_type* __s);
38 static const char_type* find(const char_type* __s, size_t __n,
39 const char_type& __a);
40
41 static char_type* move(char_type* __s1, const char_type* __s2, size_t __n);
42 static char_type* copy(char_type* __s1, const char_type* __s2, size_t __n);
43 static char_type* assign(char_type* __s, size_t __n, char_type __a);
44
not_eoftrait45 static inline int_type not_eof(int_type __c) {
46 return eq_int_type(__c, eof()) ? ~eof() : __c;
47 }
to_char_typetrait48 static inline char_type to_char_type(int_type __c) { return char_type(__c); }
to_int_typetrait49 static inline int_type to_int_type(char_type __c) { return int_type(__c); }
eq_int_typetrait50 static inline bool eq_int_type(int_type __c1, int_type __c2) {
51 return __c1 == __c2;
52 }
eoftrait53 static inline int_type eof() { return int_type(EOF); }
54 };
55
56 template <class CharT>
test()57 void test() {
58 typedef std::basic_string_view<CharT, trait<CharT> > strv_t;
59 std::hash<strv_t>
60 h; // expected-error-re 4 {{{{call to implicitly-deleted default constructor of 'std::hash<strv_t>'|implicit instantiation of undefined template}} {{.+}}}}}}
61 #if TEST_STD_VER > 17 && defined(__cpp_char8_t)
62 // expected-error-re@-2 {{{{call to implicitly-deleted default constructor of 'std::hash<strv_t>'|implicit instantiation of undefined template}} {{.+}}}}}}
63 #endif
64 (void)h;
65 }
66
main(int,char **)67 int main(int, char**) {
68 test<char>();
69 test<wchar_t>();
70 #if TEST_STD_VER > 17 && defined(__cpp_char8_t)
71 test<char8_t>();
72 #endif
73 test<char16_t>();
74 test<char32_t>();
75
76 return 0;
77 }
78