You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The **`/Zc:char8_t`** compiler option enables C++20 conforming **`char8_t`** type support. **`char8_t`** is a character type that's used to represent UTF-8 code units.
11
+
12
+
## Syntax
13
+
14
+
> **`/Zc:char8_t`**[**`-`**]
15
+
16
+
## Remarks
17
+
18
+
The **`/Zc:char8_t`** compiler option enables the **`char8_t`** type keyword as specified in the C++20 standard. It causes the compiler to generate `u8` prefixed character or string literals as `const char8_t` or `const char8_t[N]` types, respectively, instead of as `const char` or `const char[N]` types. In C++17, arrays of **`char`** may be initialized using `u8` string literals. In C++20, this initialization is ill-formed, and causes compiler error [C2440](../../error-messages/compiler-errors-1/compiler-error-c2440.md). This behavior can be a source-breaking change. You can revert the compiler to C++14 or C++17 behavior explicitly by specifying **`/Zc:char8_t-`**.
19
+
20
+
The **`/Zc:char8_t`** option is available starting in Visual Studio 2019 version 16.1. It's enabled automatically when you specify [`/std:c++20`](std-specify-language-standard-version.md) or later (such as **`/std:c++latest`**). Otherwise, it's off by default.
21
+
22
+
### Example
23
+
24
+
```cpp
25
+
constchar* s = u8"Hello"; // Compiles in C++17, Error C2440 in C++20
26
+
constchar8_t* s = u8"Hello"; // Compiles in C++20 or with /Zc:char8_t
27
+
```
28
+
29
+
### To set this compiler option in Visual Studio
30
+
31
+
1. Open the project's **Property Pages** dialog box. For details, see [Set C++ compiler and build properties in Visual Studio](../working-with-project-properties.md).
Copy file name to clipboardExpand all lines: docs/build/reference/zc-conformance.md
+1Lines changed: 1 addition & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -24,6 +24,7 @@ Here are the **`/Zc`** compiler options:
24
24
|--|--|
25
25
|[`/Zc:alignedNew`](zc-alignednew.md)| Enable C++17 over-aligned dynamic allocation (on by default in C++17). |
26
26
|[`/Zc:auto`](zc-auto-deduce-variable-type.md)| Enforce the new Standard C++ meaning for **`auto`** (on by default). |
27
+
|[`/Zc:char8_t`](zc-char8-t.md)| Enable or disable C++20 native `u8` literal support as `const char8_t` (off by default, except under **`/std:c++20`**). |
27
28
|[`/Zc:__cplusplus`](zc-cplusplus.md)| Enable the `__cplusplus` macro to report the supported standard (off by default). |
28
29
|[`/Zc:externConstexpr`](zc-externconstexpr.md)| Enable external linkage for **`constexpr`** variables (off by default). |
29
30
|[`/Zc:forScope`](zc-forscope-force-conformance-in-for-loop-scope.md)| Enforce Standard C++ **`for`** scoping rules (on by default). |
A UTF-8 encoded string is a u8-prefixed, double-quote delimited, null-terminated array of type `const char[n]`, where *n* is the length of the encoded array in bytes. A u8-prefixed string literal may contain any graphic character except the double quotation mark (`"`), backslash (`\`), or newline character. A u8-prefixed string literal may also contain the escape sequences listed above, and any universal character name.
231
234
235
+
C++20 introduces the portable **`char8_t`** (UTF-8 encoded 8-bit Unicode) character type. In C++20, `u8` literal prefixes specify characters or strings of **`char8_t`** instead of **`char`**.
236
+
232
237
```cpp
238
+
// Before C++20
233
239
constchar* str1 = u8"Hello World";
234
240
constchar* str2 = u8"\U0001F607 is O:-)";
241
+
// C++20 and later
242
+
constchar8_t* u8str1 = u8"Hello World";
243
+
constchar8_t* u8str2 = u8"\U0001F607 is O:-)";
235
244
```
236
245
237
246
### Wide string literals
@@ -259,8 +268,9 @@ A raw string literal is a null-terminated arrayβof any character typeβthat c
259
268
```cpp
260
269
// represents the string: An unescaped \ character
@@ -380,18 +391,17 @@ The actual result is a hexadecimal 5F, which is the ASCII code for an underscore
380
391
"\x05""five"// Use string splicing.
381
392
```
382
393
383
-
`std::string` literals, because they're `std::string` types, can be concatenated with the **`+`** operator that is defined for [`basic_string`](../standard-library/basic-string-class.md) types. They can also be concatenated in the same way as adjacent string literals. In both cases, the string encoding and the suffix must match:
394
+
`std::string` literals (and the related `std::u8string`, `std::u16string`, and `ste::u32string`) can be concatenated with the **`+`** operator that's defined for [`basic_string`](../standard-library/basic-string-class.md) types. They can also be concatenated in the same way as adjacent string literals. In both cases, the string encoding and the suffix must match:
384
395
385
396
```cpp
386
397
auto x1 = "hello"""" world"; // OK
387
398
auto x2 = U"hello"""L"world"; // C2308: disagree on prefix
388
-
auto x3 = u8"hello"""s u8"world"s; // OK, agree on prefixes and suffixes
389
-
auto x4 = u8"hello"""s u8"world"z; // C3688, disagree on suffixes
399
+
auto x3 = u8"hello"""s u8"world"z; // C3688, disagree on suffixes
390
400
```
391
401
392
402
### String literals with universal character names
393
403
394
-
Native (non-raw) string literals may use universal character names to represent any character, as long as the universal character name can be encoded as one or more characters in the string type. For example, a universal character name representing an extended character can't be encoded in a narrow string using the ANSI code page, but it can be encoded in narrow strings in some multi-byte code pages, or in UTF-8 strings, or in a wide string. In C++11, Unicode support is extended by the `char16_t*` and `char32_t*` string types:
404
+
Native (non-raw) string literals may use universal character names to represent any character, as long as the universal character name can be encoded as one or more characters in the string type. For example, a universal character name representing an extended character can't be encoded in a narrow string using the ANSI code page, but it can be encoded in narrow strings in some multi-byte code pages, or in UTF-8 strings, or in a wide string. In C++11, Unicode support is extended by the `char16_t*` and `char32_t*` string types, and C++20 extends it to the `char8_t` type:
395
405
396
406
```cpp
397
407
// ASCII smiling face
@@ -401,7 +411,8 @@ const char* s1 = ":-)";
401
411
constwchar_t* s2 = L"π = \U0001F609 is ;-)";
402
412
403
413
// UTF-8 encoded SMILING FACE WITH HALO (U+1F607)
404
-
constchar* s3 = u8"π = \U0001F607 is O:-)";
414
+
constchar* s3a = u8"π = \U0001F607 is O:-)"; // Before C++20
415
+
constchar8_t* s3b = u8"π = \U0001F607 is O:-)"; // C++20
405
416
406
417
// UTF-16 encoded SMILING FACE WITH OPEN MOUTH (U+1F603)
0 commit comments