Em terça-feira, 1 de novembro de 2016, às 22:16:32 PDT, Nicol Bolas escreveu:
> On Wednesday, November 2, 2016 at 12:55:46 AM UTC-4, Thiago Macieira wrote:
> > That would also not be a good idea because, unsigned char is, by
> > defintion, a
> > byte. Why should we have (more) types that mean exactly the same thing,
> > and
> > this time in all platforms, by definition?
>
> Because "unsigned char" *also* means "unsigned character". With just
> `unsigned char`, there is no way to distinguish between manipulating bytes
> and manipulating unsigned characters.
>
> That's what `byte` is for, as a type: a way to semantically differentiate
> between operations on bytes and operations on characters. The types can be
> inter-convertible, numerically speaking, but they don't mean the same thing.
I'm sorry, I don't agree that there's a distinction in the first place. Bytes
are used more often than just copying around. If you add, subtract, shift left
or right, perform bitwise operations, etc, you need the value. If I need the
value, then a zero is a zero is a zero, a 0x40 is still a 0x40.
Also, I can assign 'a' to any integer type. Maybe this was the main issue:
that single-quote character literals automatically convert to integral,
instead of staying a character. We're 40 years too late to change this, though
(since B).
> My problem with using C++ enum chicanery is that, if you use the type
> traits mechanisms to ask what `std::byte` is, it will say that it's an
> enum, not that it's an integral type. There's no reason why `byte` should
> not be an integral type.
Agreed, but I'm going to go further and say that it's pretty useless for a lot
of use-cases. I need a value of a lot of operations and an enum won't give it
to me unless I cast it to a suitable integral in the first place -- unsigned
char (that is, a *real* byte).
This week I've been spending time working on hashing algorithms, notably
SipHash (btw, implementations should reconsider their std::hash algorithms).
In order to implement it, I needed to access the byte array and do byte-level
operations like rotating left, XOR, and additions. Not only would std::byte
not work for me, I fail to see how the operations I'm doing are any different
than the operations on an unsigned char.
> It's the same reason why we have `char16_t` as a distinct type from
> `uint_least16_t`. Because there is a fundamental semantic difference
> between an array of unsigned integers that are at least 16-bits in size and
> an array of UTF-16 code units. One of this is a string; the other is not.
The only benefit I see there is allowing overloading.
But while that may be true, what's the point of an *unsigned* char? If you
want to do character operations, you use char. If you're using unsigned char,
that's because you want a byte, plain and simple. By this argument, we already
have the distinction between character operations and byte operations.