摘要:我期待的代碼:cout < < uint8_t(0);打印「0」,但它不打印任何東西。uint8_t iostream行爲
長版本:當我嘗試將uint8_t對象流到cout時,我用gcc獲取了奇怪的字符。這是預期的行爲?難道uint8_t是某些基於字符類型的別名嗎?請參閱代碼示例中的編譯器/系統註釋。
// compile and run with:
// g++ test-uint8.cpp -std=c++11 && ./a.out
// -std=c++0x (for older gcc versions)
/**
* prints out the following with compiler:
* gcc (GCC) 4.7.2 20120921 (Red Hat 4.7.2-2)
* on the system:
* Linux 3.7.9-101.fc17.x86_64
* Note that the first print statement uses an unset uint8_t
* and therefore the behaviour is undefined. (Included here for
* completeness)
> g++ test-uint8.cpp -std=c++11 && ./a.out
>>>�<<< >>>194<<<
>>><<< >>>0<<<
>>><<< >>>0<<<
>>><<< >>>0<<<
>>><<< >>>1<<<
>>><<< >>>2<<<
*
**/
#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;
for (auto i : {a,b,c,d,e,f})
{
print(i);
}
}
+1指出uint8_t可能不存在!不知道。 – Johann 2013-03-08 14:57:26
@Johann - 對於大多數用途,「uint_least8_t」是比「uint8_t」更好的選擇。 – 2013-03-08 14:59:09
允許實現提供更多擴展的整數類型,這些類型不是標準整數類型的別名。所以我認爲'uint8_t' *在技術上可能是非字符類型,儘管通常不是。 – aschepler 2013-03-08 15:00:26