固定宽度整数类型std :: uint8_t和std :: int8_t的实现,C ++ [英] Implementation of fixed width integer types std::uint8_t and std::int8_t, C++

查看:567
本文介绍了固定宽度整数类型std :: uint8_t和std :: int8_t的实现,C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用固定宽度整数类型 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和signed char之间的区别仅仅是形式上的吗?

Is the difference between char and signed char only formal?

否; 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 ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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