std :: byte指针是否具有与char *相同的别名含义? [英] Has a std::byte pointer the same aliasing implications as char*?

查看:97
本文介绍了std :: byte指针是否具有与char *相同的别名含义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++(和C)严格的别名规则包括 char * unsigned char * 可以别名

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

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

AFAIK there is no analogous rule for uint8_t*.

因此我的问题是: std :: byte 指针?

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

C ++参考当前仅指定

The C++ reference currently just specifies:


就像字符类型( char,unsigned char,signed char),它可用于访问其他对象(对象表示)占用的原始内存,但是与那些类型不同,它不是字符类型,也不是算术类型。

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):

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


对于琐碎
可复制类型T的任何对象(基类子对象除外),该对象是否持有有效的类型
T的值,组成对象的基础字节([intro.memory])可以被复制到 char,unsigned char或std :: byte
([cstddef.syn])。 43 如果将该数组的内容复制回
对象,则该对象随后应保持其原始值。 / p>

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求和一样。

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

值得一提的是([basic.lval] /8.8):

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


如果程序尝试通过以下类型之一以外的
a glvalue访问对象的存储值,则行为为
undefined:

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:


  • 一个char,unsigned char或std :: byte类型。

这篇关于std :: byte指针是否具有与char *相同的别名含义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆