我原本期望这段代码:
当我尝试将
编译并运行如下:
使用 `gcc (GCC) 4.7.2 20120921 (Red Hat 4.7.2-2)` 在系统 `Linux 3.7.9-101.fc17.x86_64` 上,产生输出:
cout << uint8_t(0);
会打印出0
,但实际上它什么都没打印出来。当我尝试将
uint8_t
对象输出到cout
时,使用gcc编译器会得到一些奇怪的字符。这是预期的行为吗?可能是因为uint8_t
是某种基于char
的类型的别名吗?请参考下面的编译器/系统说明。#include <cstdint>
#include <iostream>
void print(const uint8_t& n)
{
std::cout << ">>>" << n << "<<< "
<< ">>>" << (unsigned int)(n) << "<<<\n";
}
int main()
{
uint8_t a;
uint8_t b(0);
uint8_t c = 0;
uint8_t d{0};
uint8_t e = 1;
uint8_t f = 2;
// Note that the first print statement uses an unset uint8_t
// and therefore the behaviour is undefined. (Included here for
// completeness)
for (auto i : {a, b, c, d, e, f})
{
print(i);
}
}
编译并运行如下:
g++ test-uint8.cpp -std=c++11 && ./a.out
使用 `gcc (GCC) 4.7.2 20120921 (Red Hat 4.7.2-2)` 在系统 `Linux 3.7.9-101.fc17.x86_64` 上,产生输出:
>>>�<<< >>>194<<<
>>><<< >>>0<<<
>>><<< >>>0<<<
>>><<< >>>0<<<
>>><<< >>>1<<<
>>><<< >>>2<<<