C ++ 20的“ char8_t”与我们以前的“ char”相同吗? [英] Is C++20 'char8_t' the same as our old 'char'?

查看:240
本文介绍了C ++ 20的“ char8_t”与我们以前的“ char”相同吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在CPP参考资料中文档

我注意到 char


字符类型是大到足以代表任何UTF-8八位
代码单元(自C ++ 14起)

The character types are large enough to represent any UTF-8 eight-bit code unit (since C++14)

code> char8_t

and for char8_t


类型用于UTF-8字符表示,要求足够大
代表任何UTF-8代码单元(8位)

type for UTF-8 character representation, required to be large enough to represent any UTF-8 code unit (8 bits)

这是否意味着两种类型相同?还是 char8_t 还有其他功能?

Does that mean both are the same type? Or does char8_t have some other feature?

推荐答案

char8_t char 不同。尽管每个 unsigned char 完全相同。 noreferrer> [basic.fundamental] / 9

char8_t is not the same as char. It behaves exactly the same as unsigned char though per [basic.fundamental]/9


类型 char8_t 表示一个不同的类型,其基础类型为 unsigned char 。类型 char16_t char32_t 表示其基础类型为 uint_least16_t 和 uint_least32_t 分别在< cstdint>中。

Type char8_­t denotes a distinct type whose underlying type is unsigned char. Types char16_­t and char32_­t denote distinct types whose underlying types are uint_­least16_­t and uint_­least32_­t, respectively, in <cstdint>.

强调矿山

请注意,因为标准将其称为区别类型,例如

Do note that since the standard calls it a distinct type, code like

std::cout << std::is_same_v<unsigned char, char8_t>;

将打印 0 (假),甚至尽管 char8_t 被实现为 unsigned char 。这是因为它不是别名,而是一个不同的类型。

will print 0(false), even though char8_t is implemented as a unsigned char. This is because it is not an alias, but a distinct type.

另外要注意的是 char 可以实现为签名字符 unsigned char 。这意味着 char 可能具有与 char8_t 相同的范围和表示形式,但是它们仍然是单独的类型。 字符签名字符未签名字符 char8_t 的大小相同,但是都是不同的类型。

Another thing to note is that char can either be implemented as a signed char or unsigned char. That means it is possible for char to have the same range and representation as char8_t, but they are still separate types. char, signed char, unsigned char, and char8_t are the same size, but they are all distinct types.

这篇关于C ++ 20的“ char8_t”与我们以前的“ char”相同吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆