31

C++ (and C) strict aliasing rules include that a char* and unsigned char* may alias any other pointer.

AFAIK there is no analogous rule for uint8_t*.

Thus my question: What are the aliasing rules for a std::byte pointer?

The C++ reference currently just specifies:

Like the character types (char, unsigned char, signed char) it can be used to access raw memory occupied by other objects (object representation), but unlike those types, it is not a character type and is not an arithmetic type.

maxschlepzig
  • 35,645
  • 14
  • 145
  • 182
  • 4
    http://en.cppreference.com/w/cpp/language/reinterpret_cast#Type_aliasing The same aliasing rules apply to `std::byte` as to `char` – Justin Apr 21 '17 at 20:43
  • 2
    @Justin: You might want to be a little more clear, since "the rules apply" doesn't say which rules apply. `std::byte` has the same rules as `char` and `unsigned char`. – ShadowRanger Apr 21 '17 at 20:45
  • @Justin: Not when you first posted (you had: "en.cppreference.com/w/cpp/language/… The aliasing rules apply to `std::byte`"). You've since edited; I loaded it pre-edit. Thanks for fixing. – ShadowRanger Apr 21 '17 at 20:49

1 Answers1

24

From the current Standard draft ([basic.types.general]/2):

For any object (other than a potentially-overlapping subobject) of trivially copyable type T, whether or not the object holds a valid value of type T, the underlying bytes ([intro.memory]) making up the object can be copied into an array of char, unsigned char, or std​::​byte ([cstddef.syn]). If the content of that array is copied back into the object, the object shall subsequently hold its original value.

So yes, the same aliasing rules apply for the three types, just as cppreference sums up.

It also might be valuable to mention ([basic.lval]/11):

If a program attempts to access the stored value of an object through a glvalue whose type is not similar to one of the following types the behavior is undefined: 44

  • [...]
  • a char, unsigned char, or std​::​byte type.

44 The intent of this list is to specify those circumstances in which an object can or cannot be aliased.

Jan Schultke
  • 17,446
  • 6
  • 47
  • 96
DeiDei
  • 10,205
  • 6
  • 55
  • 80