问题描述
我正在使用固定宽度整数类型 std :: int8_t
和 std :: uint8_t
自C ++ 11起包含在C ++中(标头< cstdint>
)。我使用gcc编译器进行c ++编程(Linux,gcc --version 4.8.2)。
I'm using the fixed width integer types std::int8_t
and std::uint8_t
which are included in C++ since C++11 (header <cstdint>
). I use the gcc compiler for c++ programming (Linux, gcc --version 4.8.2).
在我的机器上,行
#include <cstdint>
#include <type_traits>
//...
std::cout << std::is_same<std::uint8_t,unsigned char>::value << std::endl;
std::cout << std::is_same<std::int8_t,char>::value << std::endl;
提供输出
1
0
换句话说: std :: uint8_t
被实现为 unsigned char
,但 std :: int8_t
不是实施为 char
!我没有(合理的)想法,为什么 std :: int8_t
没有实现为 char
。问题:如何解释此结果?
In other words: std::uint8_t
is implemented as unsigned char
but std::int8_t
is not implemented as char
! I have no (reasonable) idea how it could be that std::int8_t
is not implemented as char
. Question: How can this result be interpreted?
推荐答案
可能他们使用了签名字符
,因为更改编译器选项(在gcc -fsigned-char
和 -funsigned-char
中)是明智的普通 char
的签名。
Probably they used signed char
, which is sensible since compiler options (in gcc -fsigned-char
and -funsigned-char
) can change the signedness of plain char
.
请注意,因此,即使在编译器上对 char
进行了签名,这也很正常,即使它与 signed char
。
Notice that char
, signed char
and unsigned char
are guaranteed to be distinct types, so it's normal that, even if on your compiler char
is signed, it isn't considered the same as signed char
.
否; char
可以签名,也可以不签名,具体取决于编译器和编译器选项; 带符号的字符
始终是带符号的,无论如何。
No; char
can be signed or unsigned depending on compiler and compiler options; signed char
is always signed, no matter what.
(现在,如果您问我,普通的 char
应该始终为 unsigned
,但这只是我的意见)
(now, if you ask me, plain char
should always be unsigned
, but that's just my opinion)
这篇关于固定宽度整数类型std :: uint8_t和std :: int8_t的实现,C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!