对对象的引用构成访问吗?
Does forming a reference to an object constitute access?
GCC和Clang当前正在做的事情:
Here's what GCC and Clang currently do:
void test(int const volatile* ptr) noexcept { *ptr; // movl (%rdi), eax // Reads *ptr [[maybe_unused]] int const volatile& ref = *ptr; // Does not read *ptr }我的问题是关于陈述的
[[maybe_unused]] int const volatile& ref = *ptr;- 根据抽象机器,这是否读取了 ptr 指向的对象的值?
- 如果 ptr == nullptr ,此语句是否孤立地成为未定义的行为?
- 是的,对空指针的间接寻址是UB- stackoverflow/a/59205697/1614051
- According to the abstract machine, does this read the value of the object pointed to by ptr?
- Would this statement, in isolation, be undefined behavior if ptr == nullptr?
- Yes, indirection on null pointer is UB - stackoverflow/a/59205697/1614051
- int const volatile&是吗?ref = * ptr; 读取指向对象的值?
- 不.
- 是的,未定义空指针上的 * ptr .
- 不,仅构成引用并不违反严格的别名.
- 大概是 reinterpret_cast -引用正确的类型是允许的和有效的.
- No, just forming the reference does not violate strict aliasing.
- Presumably reinterpret_cast-ing the reference to the correct type is allowed and valid.
[basicpound]/3 ...指针类型的每个值都是以下之一:
[basicpound]/3 ... Every value of pointer type is one of the following:
(3.1)-指向对象或函数的指针(据说该指针指向该对象或函数),或者
(3.1) — a pointer to an object or function (the pointer is said to point to the object or function), or
(3.2)-超出对象末尾的指针(8.7),或者
(3.2) — a pointer past the end of an object (8.7), or
(3.3)-该类型的空指针值(7.11),或者
(3.3) — the null pointer value (7.11) for that type, or
(3.4)—无效的指针值.
(3.4) — an invalid pointer value.
[expr.unary.op]/1 一元 * 运算符执行 indirect :应用该表达式的表达式应为指向对象类型的指针,或者指向函数类型的指针,结果是一个左值,表示表达式所指向的对象或函数.
[expr.unary.op]/1 The unary * operator performs indirection: the expression to which it is applied shall be a pointer to an object type, or a pointer to a function type and the result is an lvalue referring to the object or function to which the expression points.
因此,表达式 * ptr 的含义仅针对指向对象或函数的指针 ptr 定义,即,其值小于 [basicpound]/(3.1).在所有其他情况下,此表达式都表现出不确定的行为.
Thus, the meaning of the expression *ptr is only defined for pointer ptr that points to an object or function - that is, a pointer whose value falls under [basicpound]/(3.1). In all other cases, this expression exhibits undefined behavior.
请注意,我专门询问有关形成引用的问题,而不是有关使用它来读取值的问题.
Note that I ask specifically about forming the reference, and not about using it to read the value.
编辑09/12/2019:接受以下答案:
更多推荐
形成对对象的引用是否构成访问?
发布评论