为什么一元运算符 & 不需要完整类型?

Why does unary operator & not require a complete type?

以下代码在 gcc 7.2.0 and clang 6.0.0.

下都可以正常编译
#include <iostream>

struct stru;

void func(stru& s) {
  std::cout << &s << std::endl;
}

int main() {

}

我想知道这是怎么回事。如果 stru 超载了 operator&() 怎么办?编译器不应该仅仅通过像 struct stru 这样的前向声明来判断。在我看来,只有 std::addressof(s) 可以使用不完整的类型。

What if stru has overloaded operator&()?

然后未指定是否调用重载(标准引用见Oliv的评论)。

How could unary operator & does not require a complete type?

标准就是这样定义语言的。 built-in address-of 运算符不需要知道类型的定义,因为这对从何处获取对象地址没有影响。

为什么这是一件好事的一个考虑因素:与 C 的兼容性。