-1

What exactly is uint8_t made for? If it remains indistinguishable from unsigned char and cannot be used to overload functions?

I found many answers in this post : std::cout deal with uint8_t as a character

So I've reedited the question entirely and re-titled the question.

Maelstrom
  • 9
  • 4
  • Please read [ask] with a [mcve] – Richard Critten Feb 01 '20 at 12:31
  • there are countless number of duplicates: [std::cout deal with uint8_t as a character](https://stackoverflow.com/q/39145753/995714), [Behavior of cout << hex with uint8 and uint16](https://stackoverflow.com/q/23575381/995714), [Printing uint8_t variables using std::cout in C++](https://stackoverflow.com/q/30856861/995714)... – phuclv Feb 01 '20 at 13:54
  • No, it doesn't. I really think we should be allowed to overload functions on other criteria than the length and sign of types in c++. Compilers could still be smarter. – Maelstrom Feb 01 '20 at 14:03
  • The point is that you can't overload a function on different type-*aliases*, you need to have a [different type](https://wandbox.org/permlink/CRcvWAt1q13GDEEs). – Bob__ Feb 01 '20 at 15:58
  • don't edit the question to modify its meaning entirely. Ask another question if you want – phuclv Feb 01 '20 at 16:36

2 Answers2

4

On all systems with 8-bit bytes, they are variants of char. This includes the type-aliases for e.g. uint8_t (which is a type-alias for unsigned char on such a system).

And no matter what type-alias you have, char (and unsigned char and signed char (yes those are three distinctive types)) will be treated as characters by the stream output operator <<.

If you want to print the integer value of any char based type you need to cast it to e.g. int. As in

std::cout << static_cast<unsigned>(my_uint8_t_var) << '\n';

As a side-note: There are systems which doesn't have 8-bit bytes. On such systems type-aliases like uint8_t are not possible, and does not exist. If you see e.g. this fixed-width integer reference you will see that the exact fixed-width integer types are optional.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • I see the purpose of portability. But casting is bothering me, static_cast(my_uint8_t_var) will resize my variable I suppose and there must be an overhead somewhere when handling bytes of files or vectors – Maelstrom Feb 01 '20 at 13:49
  • @Maelstrom Casting will not change the size, type or value of the variable itself, only the result of the cast will be e.g. `unsigned`. You can of course [read](https://en.cppreference.com/w/cpp/io/basic_istream/read) or [write](https://en.cppreference.com/w/cpp/io/basic_ostream/write) raw data, but then that will be a bitwise copy and not a translation into the textual representation of the value. – Some programmer dude Feb 01 '20 at 16:03
0

uint8_t is an 8 bit unsigned integer. For storing values from 0 to 255. It usually has the same range as an unsigned char but is one of several explicitly sized integer types available with cstdint

w08r
  • 1,639
  • 13
  • 14