Are all temporaries rvalues in C++?

前端 未结 7 902
隐瞒了意图╮
隐瞒了意图╮ 2020-12-05 04:56

I have been coding in C++ for past few years. But there is one question that I have not been able to figure out. I want to ask, are all temporaries in C++, rvalues?

7条回答
  •  遥遥无期
    2020-12-05 05:27

    It depends on what you consider a temporary variable is. You can write something like

    #include 
    int main()
    {
        char carray[10];
        char *c=carray+1;
        *(c+2+4) = 9;
        printf("%d\n",carray[7]);
        return 0;
    }
    

    This runs in VisualStudios and GCC. You can run the code in codepad

    I consider (c+2+4) a rvalue although i want to assign to it. When i dereference it, it would become an lvalue. So yes all temporaries are rvalues. But you can make rvalues (thus a temporary) into an lvalue by dereferencing it

提交回复
热议问题