How to use new std::byte type in places where old-style unsigned char is needed?

后端 未结 3 1842
无人及你
无人及你 2020-12-07 20:12

std::byte is a new type in C++17 which is made as enum class byte : unsigned char. This makes impossible to use it without appropriate conversion.

3条回答
  •  心在旅途
    2020-12-07 21:10

    If you want something that behaves like a byte in the way you'd probably expect it but is named distinctly different from unsigned char use uint8_t from stdint.h. For almost all implementations this will probably be a

    typedef unsigned char uint8_t;
    

    and again an unsigned char under the hood - but who cares? You just want to emphasize "This is not a character type". You just don't have to expect to be able to have two overloads of some functions, one for unsigned char and one for uint8_t. But if you do the compiler will push your nose onto it anyway...

提交回复
热议问题