gpt4 book ai didi

c++ - 在 C++11 标准中,为什么要保留 char 类型的实现依赖?

转载 作者:搜寻专家 更新时间:2023-10-31 00:24:34 26 4
gpt4 key购买 nike

背景

一些 C++ 源 Material 和堆栈溢出问题讨论了 char 的实现依赖性。也就是说,C++ 中的 char 可以定义为 unsigned charsigned char,但此实现 depends entirely on the compiler根据 ARM Linux FAQ:

The above code is actually buggy in that it assumes that the type "char" is equivalent to "signed char". The C standards do say that "char" may either be a "signed char" or "unsigned char" and it is up to the compilers implementation or the platform which is followed.

这为歧义问题和不良做法(包括 mistaking the signage of a char)打开了大门当用作 8 位数字时。 Rationale for C提供了为什么会出现这种情况的一些原因,但没有解决留下歧义可能性的问题:

Three types of char are specified: signed, plain, and unsigned. A plain char may be represented as either signed or unsigned, depending upon the implementation, as in prior practice. The type signed char was introduced to make available a one-byte signed integer type on those systems which implement plain char as unsigned. For reasons of symmetry, the keyword signed is allowed as part of the type name of other integral types.

关闭甚至可能出现歧义的大门,只保留 unsigned charsigned char 类型作为 8 的两种数据类型似乎是有利的-位单位。这促使我提出问题...

问题

鉴于可能存在歧义,为什么要让 char 数据类型实现依赖?

最佳答案

有些处理器更喜欢 signed char,而其他处理器更喜欢 unsigned char。例如,POWER 可以从零扩展的内存中加载一个 8 位值,但不能加载符号扩展。但是 SuperH-3 可以从内存中加载一个带符号扩展但不能为零扩展的 8 位值。 C++ 派生自 C,C 保留了语言实现定义的许多细节,因此每个实现都可以针对其目标环境进行定制以使其最有效。

关于c++ - 在 C++11 标准中,为什么要保留 char 类型的实现依赖?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57898617/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com