File: char8_t.cpp

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (90 lines) | stat: -rw-r--r-- 2,311 bytes parent folder | download | duplicates (14)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// RUN: %clang_cc1 -fchar8_t -std=c++17 -verify %s
// RUN: %clang_cc1 -std=c++2a -verify=expected %s
// RUN: %clang_cc1 -std=c++2a -verify=expected -fno-signed-char %s


char8_t a = u8'a';
char8_t b[] = u8"foo";
char8_t c = 'a';
char8_t d[] = "foo"; // expected-error {{initializing 'char8_t' array with plain string literal}} expected-note {{add 'u8' prefix}}

char e = u8'a';
char g = 'a';
char h[] = "foo";

unsigned char i[] = u8"foo";
unsigned char j[] = { u8"foo" };
char k[] = u8"foo";
char l[] = { u8"foo" };
signed char m[] = u8"foo"; // expected-error {{initialization of char array with UTF-8 string literal is not permitted}}
signed char n[] = { u8"foo" }; // expected-error {{cannot initialize an array element of type 'signed char' with an lvalue of type 'const char8_t[4]'}}

const unsigned char* uptr = u8"foo"; // expected-error {{cannot initialize}}
const signed char* sptr = u8"foo"; // expected-error {{cannot initialize}}
const char* ptr = u8"foo"; // expected-error {{cannot initialize}}

template <typename T>
void check_values() {
  constexpr T c[] = {0, static_cast<T>(0xFF), 0x42};
  constexpr T a[] = u8"\x00\xFF\x42";

  static_assert(a[0] == c[0]);
  static_assert(a[1] == c[1]);
  static_assert(a[2] == c[2]);
}

void call_check_values() {
  check_values<char>();
  check_values<unsigned char>();
}

void disambig() {
  char8_t (a) = u8'x';
}

void operator""_a(char);
void operator""_a(const char*, decltype(sizeof(0)));

void test_udl1() {
  int &x = u8'a'_a; // expected-error {{no matching literal operator}}
  float &y = u8"a"_a; // expected-error {{no matching literal operator}}
}

int &operator""_a(char8_t);
float &operator""_a(const char8_t*, decltype(sizeof(0)));

void test_udl2() {
  int &x = u8'a'_a;
  float &y = u8"a"_a;
}

template<typename E, typename T> void check(T &&t) {
  using Check = E;
  using Check = T;
}
void check_deduction() {
  check<char8_t>(u8'a');
  check<const char8_t(&)[5]>(u8"a\u1000");
}

static_assert(sizeof(char8_t) == 1);
static_assert(char8_t(-1) > 0);
static_assert(u8"\u0080"[0] > 0);

namespace ambiguous {

struct A {
	char8_t s[10];
};
struct B {
  char s[10];
};

void f(A); // expected-note {{candidate}}
void f(B); // expected-note {{candidate}}

int test() {
  f({u8"foo"}); // expected-error {{call to 'f' is ambiguous}}
}

}