microsoft / GSL

Guidelines Support Library
Other
6.13k stars 737 forks source link

Question: How to print a gsl::byte? #1091

Closed honey-speaks-tech closed 1 year ago

honey-speaks-tech commented 1 year ago

Hi all, We are using C++14 and we are happily using Microsoft GSL library (ms-gsl 4.0.0). We are trying an example to print some gsl::byte values. Here is the godbolt link: https://godbolt.org/z/5cKbvWsK3

For some reason, we are yet not able to figure out how gsl::byte could be used and printed in our codebase. Could you please help us sort out this issue? In the example, we would like to print both individual gsl::byte and also gsl::byte values in the array. We need to use this gsl::byte variables in our unit tests as well with google test macros such as EXPECT* or ASSERT*.

We would definitely appreciate any leads on this.

Thank you, Honey

Honey Sukesan

dmitrykobets-msft commented 1 year ago

Hi @honey-speaks-tech,

I tweaked your example to something that works for me: https://godbolt.org/z/8c11Gv495. The main difference in implementation is that in v1, the byte is converted into an int type as opposed to std::uint8_t, since doing the latter will attempt to interpret the byte value as an ASCII character, as opposed to a numerical value. See this discussion.

If this doesn't answer your question, could you please provide an expected output alongside the output you are seeing?

honey-speaks-tech commented 1 year ago

Hi @dmitrykobets-msft, Sorry for the delay in response. This perfectly answers my question. Thank you for the support.

Best, Honey

Honey Sukesan