是右值结构的成员是右值还是左值? [英] Is a member of an rvalue structure an rvalue or lvalue?

查看:120
本文介绍了是右值结构的成员是右值还是左值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

返回结构的函数调用是一个右值表达式,但是它的成员呢?

这段代码适用于我的g ++编译器,但是gcc给出一个错误说需要左值作为左操作数of assignment:

  struct A 
{
int v;
};

struct A fun()
{
struct A tmp;
return tmp;
}

int main()
{
fun()。v = 1;
}

gcc treats fun()。v 作为右值,我可以理解。

但是g ++不认为赋值表达式是错误的。这意味着fun1()。v是C ++中的左值?

现在的问题是,我搜索C ++ 98/03标准,没有发现是否 fun .v 是左值或右值。

那么,是什么?

解决方案

p> $ r

5.3.5 [expr.ref]中的标准状态:


如果E2被声明为类型
引用T,则E1.E2是
lvalue [...]
- 如果E2是非静态数据成员,并且E1的类型是cq1 vq1 X,而
的类型是cq2 vq2 T,则
表达式指定命名成员
由第一个
表达式指定的对象。如果E1是一个左值,那么
E1.E2是一个左值。



A function call returning a structure is an rvalue expression, but what about its members?
This piece of code works well with my g++ compiler, but gcc gives a error saying "lvalue required as left operand of assignment":

struct A
{
    int v;
};

struct A fun()
{
    struct A tmp;
    return tmp;
}

int main()
{
    fun().v = 1;
}

gcc treats fun().v as rvalue, and I can understand that.
But g++ doesn't think the assignment expression is wrong. Does that mean fun1().v is lvalue in C++?
Now the problem is, I searched the C++98/03 standard, finding nothing telling about whether fun().v is lvalue or rvalue.
So, what is it?

解决方案

A member of an rvalue expression is an rvalue.

The standard states in 5.3.5 [expr.ref]:

If E2 is declared to have type "reference to T", then E1.E2 is an lvalue [...] - If E2 is a non-static data member, and the type of E1 is "cq1 vq1 X", and the type of E2 is "cq2 vq2 T", the expression designates the named member of the object designated by the first expression. If E1 is an lvalue, then E1.E2 is an lvalue.

这篇关于是右值结构的成员是右值还是左值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆