gpt4 book ai didi

c++ - 涉及函数类型的表达式是左值还是右值?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:52:46 26 4
gpt4 key购买 nike

void fn() {}
void (&lref)() = fn;
void (&&rref)() = fn;

int main() {}

在 g++ 4.8.1 下编译良好。

所以,fn 是一个表达式,根据 ISO 标准,表达式必须有一个类别。

在执行任何自动类型提升之前,表达式属于哪个类别(因为两个引用都可以接受表达式 fn 的计算结果)?

最佳答案

根据 C++11 3.10/1,函数始终是左值。所以表达式 fn 是一个左值。

根据 8.5.3/5,正确类型的函数可用于初始化右值引用:

  • Otherwise, the reference shall be an lvalue reference to a non-volatile const type (i.e., cv1 shall be const), or the reference shall be an rvalue reference.

    • If the initializer expression

      • is an xvalue, class prvalue, array prvalue or function lvalue and “cv1 T1” is reference-compatible with “cv2 T2”, or
      • ...

      then the reference is bound to the value of the initializer expression in the first case ...

(缩短,强调我的)

关于c++ - 涉及函数类型的表达式是左值还是右值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29345522/

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