std::cout deal with uint8_t as a character

前端 未结 3 1554
温柔的废话
温柔的废话 2020-12-21 06:06

If I run this code:

std::cout << static_cast(65);

It will output:

A

Which

3条回答
  •  轮回少年
    2020-12-21 06:19

    Posting an answer as there is some misinformation in comments.

    The uint8_t may or may not be a typedef for char or unsigned char. It is also possible for it to be an extended integer type (and so, not a character type).

    Compilers may offer other integer types besides the minimum set required by the standard (short, int, long, etc). For example some compilers offer a 128-bit integer type.

    This would not "conflict with C" either, since C and C++ both allow for extended integer types.

    So, your code has to allow for both possibilities. The suggestion in comments of using unary + would work.

    Personally I think it would make more sense if the standard required uint8_t to not be a character type, as the behaviour you have noticed is unintuitive.

提交回复
热议问题