当前位置:网站首页>[character set 8] char8_ t、char16_ t、char32_ t、wchar、char

[character set 8] char8_ t、char16_ t、char32_ t、wchar、char

2022-06-12 08:52:00 junxuezheng

One 、vs How the compiler supports C++20 standard

Reference resources :

Two 、 brief introduction

3、 ... and 、demo

#include <iostream>
using namespace std;
int main()
{
    
    //char* loc = setlocale(LC_ALL,".65001");

    char ch[] = " I ";
    char8_t ch8[] = u8" I ";
    wchar_t wch[] = L" I ";
    char16_t ch16[] = u" I ";
    char32_t ch32[] = U" I ";

    cout << &ch<<endl;
    cout << &ch8 << endl;
    cout << &wch << endl;
    cout << &ch16 << endl;
    cout << &ch32 << endl<<endl;

    /*cout << ch << endl; wcout << wch << endl;*/
    printf("ch:%#x", ch[0]); printf("%#x", ch[1]); printf("%#x", ch[2]); printf("%#x", ch[3]);
    printf("\n");

    printf("ch8:%#x", ch[0]); printf("%#x", ch8[1]); printf("%#x", ch8[2]); printf("%#x", ch8[3]);
    printf("\n");

    printf("wch:%#x", wch[0]);
    printf("\n");

    printf("ch16:%#x", ch16[0]);
    printf("\n");

    printf("ch32:%#x", ch32[0]);
    printf("\n");
}

Output :

00B7F83C
00B7F830
00B7F824
00B7F818
00B7F808
ch:0xffffffe60xffffff880xffffff910
ch8:0xffffffe60x880x910
wch:0x6211
ch16:0x6211
ch32:0x6211
 Insert picture description here

utf-8 Is one Chinese character three bytes long ?

Four 、 analysis

5、 ... and 、c++20 standard :char8_t Can't output

Recap
char8_t can not hold a full range of utf-8 glyphs. utf-8 glyphs can be up to 4 bytes. char8_t holds up to 1 byte.
char8_t stream out is expressly forbidden in C++20
for utf-8 compatibility while in VStudio use char and the /utf-8 switch
in case you really want C++20 way of transforming to/from char8_t, you need to use … AFAIK not yet fully implemented in any of the 3, as required by the standard

See :C++20 char8_t, the Boson

原网站

版权声明
本文为[junxuezheng]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206120842241338.html