No. Don’t cast unless you have already accounted for endianness issues.
Also, don’t cast unless you can do the proper end-run around your compiler’s strict language standards checking — C++ doesn’t want you to do that, because it’s dangerous code. IDR whether it is technically UB or not. (It probably is.)
apart from endianness, which simply makes the bytes not where you might think they would be in the array indexing, its fine. You can do a one time (per execution) test to see which endian your machine is and proceed as necessary from that knowledge. Its not different than casting it to a char* to use it with a binary file write or similar commonly done pointer magic operations.
But seems to fail.
be very specific or show your code.
try printing it in [3][2][1][0] order and see if you get your 123456 back... that is the endian issue, you were getting 56 34 12 probably?
this works fine here in the forum compiler thingy.
Take note that it C++ specifically indicates this is a one-way operation.
All that said, you can generally get away with it —I have— but again, know your compiler and know your target architecture, otherwise all bets are off. UB is not your friend.
For the cast to be correct, u8 must be another name for unsigned char or char or std::byte. It can't be anything else. In particular it can't be char8_t.
In my opinion u8 should not be std::uint8_t either, since the implementation has freedom to define it as something besides one of the acceptable types. See this: https://cplusplus.com/forum/general/284814/#msg1234780