加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C 11中的变量表达式?

发布时间:2020-12-16 10:33:29 所属栏目:百科 来源:网络整理
导读:在C 11中,我对类型T和对T的引用之间的区别感到困惑,因为它们适用于命名变量的表达式.具体考虑: int main(){ int x = 42; int y = x; x; // (1) y; // (2)} 上面(1)中的表达式x的类型是什么?是int还是lvalue对int的引用? (它的值类别显然是左值,但这与其类
在C 11中,我对类型T和对T的引用之间的区别感到困惑,因为它们适用于命名变量的表达式.具体考虑:

int main()
{
    int x = 42;
    int& y = x;

    x; // (1)
    y; // (2)
}

上面(1)中的表达式x的类型是什么?是int还是lvalue对int的引用? (它的值类别显然是左值,但这与其类型是分开的)

同样,上面(2)中的表达式y的类型是什么?是int还是lvalue对int的引用?

它在5.1.1.8中说:

The type of [an identifier primary expression] is the type of the identi?er.
The result is the entity denoted by the identi?er. The result is an lvalue if the entity is a function,variable,or data member and a prvalue otherwise.

解决方法

你缺少的是这个(§5/ 5):

If an expression initially has the type “reference to T” (8.3.2,8.5.3),the type is adjusted to T prior to any further analysis.

因此,虽然标识符y的类型为int&,但表达式y的类型为int.表达式永远不会有引用类型,因此表达式的类型都是int.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读