形成对对象的引用是否构成访问?

Does forming a reference to an object constitute access?

形成对对象的引用是否构成访问?

这是 GCC 和 Clang 目前所做的:

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;

请注意,我特别询问了关于形成参考的问题,而不是关于使用它来读取值的问题。

编辑 09/12/2019:接受以下答案:

[basic.compound]/3 ... Every value of pointer type is one of the following:

(3.1) — a pointer to an object or function (the pointer is said to point to the object or function), or

(3.2) — a pointer past the end of an object (8.7), or

(3.3) — the null pointer value (7.11) for that type, or

(3.4) — an invalid pointer value.


[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 定义 - 即,其值属于 [ 的指针basic.compound]/(3.1)。在所有其他情况下,此表达式表现出未定义的行为。