请在此澄清左值和右值的概念

时间:2016-08-16 19:48:11

标签: c++ c

我有下面给出的程序

customDF <- eventReactive(input$buildTable,{

    p <- getPoly()
    a <- attrib()

    for( i in 1:length(activeLayers)){
      word <- activeLayers[i]
      p[[word]] <- with(a, a[[word]][match(p$geoid, a$geoid)])
    }
    p 
  })

 # Method that uses it
 exportResult <- eventReactive(input$export, {        
    j <- customDF()
    writeSpatialShape(j, paste(repoLocation,input$layerName, sep=""))

    "Export successful"
  })

importResult <- eventReactive(input$selectImport, {
    # ?????? 
  })

这个程序在c ++中编译得很好但在c中却没有。我也无法真正理解。但我尝试过阅读和搜索,发现这是因为preincrement运算符在c和l值中返回rvalue。

如果我将#include<stdio.h> int main() { int i=5; (++(++i)); } 更改为(++(++i)),则c和c ++中的编译都会失败,因为post-increment总是返回rvalue。

即使经过一些阅读,我也无法清楚地了解左撇子和右撇子的含义。有人可以用外行的话来解释这些是什么吗?

4 个答案:

答案 0 :(得分:3)

在后缀或前缀++中,运算符要求操作数是可修改的左值。两个运算符都执行左值转换,因此对象不再是左值。

C ++还要求前缀++运算符的操作数是可修改的左值,但前缀++运算符的结果是左值。对于postfix ++运算符,情况并非如此。

因此(++(++i));编译,因为第二个操作获得了左值,但(++(i++))没有。

答案 1 :(得分:2)

  

&#34;左值(定位符值)表示占据内存中某些可识别位置的对象(即具有地址)。

     

rvalues由排除定义,表示每个表达式都是左值或右值。因此,从上面的左值定义来看,右值是一个表达式,它不代表占据内存中某些可识别位置的对象。&#34;

参考:http://eli.thegreenplace.net/2011/12/15/understanding-lvalues-and-rvalues-in-c-and-c

根据该定义, 后增量i++不再适用,因为返回的表达式不再位于i中,因为它已递增。

同时++i返回的表达式返回对递增变量i的引用

答案 2 :(得分:1)

Rvalues基本上是原始值或操作的临时结果,不应对它们进行操作。因此,不允许对这些进行增量前或增量后操作。

Lvalues是传统的值,指的是存储的对象,函数或原语,可以操作或调用的东西。

在第一行:int i=5; // i is an lvalue, 5 is an rvalue

因此,++(i++)正在转换为++6,这基本上就是编译器所抱怨的。

顺便说一下,这已在这里得到解答:What are rvalues, lvalues, xvalues, glvalues, and prvalues?

答案 3 :(得分:0)

"lvalue" and "rvalue" are so named because of where each 
  of them can appear in an assignment operation.  An 
  lvalue 
 can appear on the left side of an assignment operator, 
  whereas an rvalue can appear on the right side.

  As an example:

int a;
a = 3;

In the second line, "a" is the lvalue, and "3" is the rvalue.

 in this example:



int a, b;
a = 4;
b = a;

In the third line of that example, "b" is the lvalue, and "a" is 
the rvalue, whereas it was the lvalue in line 2.  This 
illustrates an important point: An lvalue can also be an 
rvalue, but an rvalue can never be an lvalue.

Another definition of lvalue is "a place where a value can 
be stored."