gpt4 book ai didi

c++ - 具有与 char* 相同的别名含义的 std::byte 指针吗?

转载 作者:IT老高 更新时间:2023-10-28 12:58:07 27 4
gpt4 key购买 nike

C++(和 C)严格的别名规则包括 char*unsigned char* 可以别名任何其他指针。

AFAIK 对于 uint8_t* 没有类似的规则。

因此我的问题是:std::byte 的别名规则是什么?指针?

C++ 引用目前只是 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.

最佳答案

来自当前的标准草案([basic.types]/2):

For any object (other than a base-class 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]).43 If the content of that array is copied back into the object, the object shall subsequently hold its original value.

是的,相同的别名规则适用于这三种类型,正如 cppreference 总结的那样。

提及 ([basic.lval]/8.8) 也可能很有值(value):

If a program attempts to access the stored value of an object through a glvalue of other than one of the following types the behavior is undefined:

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

关于c++ - 具有与 char* 相同的别名含义的 std::byte 指针吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43551665/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com