背景:
我有模板流运算符(例如operator << (ostream &, std::vector <T>)
)(输出可能是某种 8 位整数类型的容器元素(例如unsigned char
,,int_least8_t
等等)。
问题:
默认是这些类型输出为char
(ASCII)。我只使用char
(或wchar_t
其他)ASCII 变量,从不使用无符号/有符号类型。即使调用者不知道类型,如何让这些其他 8 位类型始终输出为signed int
/ (数字)?unsigned int
第一次尝试:
例如,我尝试过(使用 GCC)在其中使用强制转换进行定义operator << (ostream &, unsigned char)
(即stream << static_cast <int> (value)
. 这适用于unsigned char
值,但随后uint8_t
仍将输出作为char
.
相同的底层类型(即unsigned/signed char
不能在重载中使用,所以我不能定义例如operator << (ostream &, int_fast8_t)
.