关于左值到右值的转换

发布于 2024-10-02 22:58:59 字数 145 浏览 10 评论 0原文

“一元 & 运算符的操作数上未完成左值到右值的转换。”

我可以知道它的含义吗>任何人都可以解释一下..请

例如:

 int a[]={1,5};
 int* x=&a; 

"lvalue-to-rvalue conversion is not done on the operand of the unary & operator."

May I know what it meant for >Can any one explain ..It Please

Ex:

 int a[]={1,5};
 int* x=&a; 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

人│生佛魔见 2024-10-09 22:58:59

嗯,这意味着左值到右值的转换不是
完毕:-)。当您需要时,会发生左值到右值的转换
对象的值,而不仅仅是对其的引用。
获取对象的地址不需要值,因此
没有左值到右值的转换。

我不确定这与您的代码有何关系。转换
代码中的第二行没有完成的是
数组到指针的转换。左值到右值的转换
当然,也没有完成,但这是相当直观的
和正常的。数组到指针的转换发生在大多数用途中
然而,表达式中的数组,因此可能会令人惊讶
有些人更多。 (实际上,数组到指针的转换
仅在必要时发生。但也有非常非常少
对数组合法的操作,所以经常发生
必要的。)在一元 & 的情况下运算符,数组
指针转换不会发生(也不能发生,因为
数组到指针转换的结果是右值,并且
一元和需要一个左值)。所以你要获取的地址是
数组,其类型为 int ()[2],即指向数组的指针
两个整数。这反过来又使分配非法,因为
目标类型不是 int (
)[2],而是 int*,并且没有
两者之间的隐式转换。

Well, it means that the lvalue to rvalue conversion isn't
done:-). An lvalue to rvalue conversion occurs when you need
the value of an object, rather than just a reference to it.
Taking the address of an object doesn't require the value, so
there is no lvalue to rvalue conversion.

I'm not sure how this is relevant to your code. The conversion
which isn't being done in the second line in your code is the
array to pointer conversion. The lvalue to rvalue conversion
isn't being done either, of course, but that's rather intuitive
and normal. The array to pointer conversion occurs in most uses
of an array in an expression, however, and so might surprise
some people more. (Actually, the array to pointer conversion
only occurs when necessary. But there are very, very few
operations which are legal on an array, so it's very often
necessary.) In the case of a unary & operator, the array to
pointer conversion doesn't take place (and can't, since the
result of the array to pointer conversion is an rvalue, and
unary & requires an lvalue). So you're taking the address of
the array, which has type int ()[2], i.e. pointer to an array
of two int. Which in turn makes the assignment illegal, since
the target type is not int (
)[2], but int*, and there's no
implicit conversion between the two.

木槿暧夏七纪年 2024-10-09 22:58:59

在表达式中,左值右值的转换是指查看对象的值,并在需要对象值的地方使用(例如在a + b中) 您需要 ab 的值来确定结果,您不需要知道原始对象来自何处(如果有的话)。

address-of运算符中,您需要拥有对象本身(即左值),对象的值是不相关的,因此左值右值的转换是没有帮助的,它会丢失对象本身的身份(位置),而这对于获取地址来说是很重要的。

In an expression, an lvalue to rvalue conversion refers to looking at the value of an object and is used where you need an object value (e.g. in a + b you need the values of a and b to determine the result you don't need to know where - if anywhere - the original objects came from).

In the address-of operator you need to have the object itself (i.e. an lvalue), the value of the object is irrelevant so an lvalue to rvalue conversion would be unhelpful, it would lose the identity (location) of the object itself which is what is important for taking an address.

自找没趣 2024-10-09 22:58:59
int a[] = {1, 5};
int* x = &a;

这是一个类型错误。 & 运算符产生一个指向其操作数的指针。这里它的操作数是什么? 2 个整数的数组。指向由 2 个整数组成的数组的指针不是 int* 类型,而是 int(*)[2] 类型。

请注意您引用的规则在此处的应用方式:在许多上下文中,数组的名称会衰减为指向其第一个元素的指针。 (但一般来说,数组和指针相同!)这就是当您说 <代码> int * x = a; 。但是,当您将 & 运算符应用于数组时,这种衰减不会发生。 (如果该转换确实发生,&a将尝试获取a的地址,这是无意义的。)

int a[] = {1, 5};
int* x = &a;

That's a type error. The & operator yields a pointer to its operand. What's its operand here? An array of 2 integers. A pointer to an array of 2 integers is not of type int*, but of type int(*)[2].

Note how the rule you quoted applies here: in many contexts, the name of an array decays to a pointer to its first element. (But generally, arrays and pointers are not the same!) That's what happens when you say int* x = a;. But this decay does not happen when you apply the & operator to an array. (If that conversion did happen, &a would try to take the address of the address of a, which is nonsense.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文