【发布时间】:2014-08-10 15:50:05
【问题描述】:
我正在使用 固定宽度整数类型 std::int8_t 和 std::uint8_t,它们自 C++11 起包含在 C++ 中(标题 <cstdint>)。我使用 gcc 编译器进行 c++ 编程(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。问题:如何解释这个结果?
【问题讨论】:
-
试试
std::is_same<std::int8_t, signed char>。 -
char可以在某些系统上签名。请尝试与signed char进行比较。 -
@Matteo Italia + 0x499602D2:对于签名的字符,它给出 1,你是对的。 char 和 signed char 的区别只是形式上的吗?
-
@sperber:你为什么这么说?听起来您认为
char总是签名的,但事实并非如此。 1) 可以通过编译器标志来改变; 2) 它甚至不一定在所有系统上都签名默认。char的签名完全由实现定义。 -
@LightnessRacesinOrbit:谢谢,我不知道它是实现定义的,确实认为它需要签名。