Are all temporaries rvalues in C++?

前端 未结 7 895
隐瞒了意图╮
隐瞒了意图╮ 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:16

    well, that array operator returns a reference, any function that returns a reference could be considered to do the same thing? all references are const, while they can be lvalues, they modify what they reference, not the reference itself. same is true for the *operator,

    *(a temp pointer) = val;

    I swear I used to use some compiler that would pass temp values to any function that took a reference,

    so you could go:

    int Afunc()
    {
       return 5;
    }
    
    int anotherFunc(int & b)
    {
        b = 34;
    }
    
    
    anotherFunc(Afunc());
    

    can't find one that lets you do that now though, the reference has to be const in order to allow passing of temp values.

    int anotherFunc(const int & b);
    

    anyway, references can be lvalues and temporary, the trick being the reference it's self is not modified, only what it references.

    if you count the-> operator as an operator, then temporary pointers can be lvalues, but the same condition applies, its not the temp pointer that would be changed, but the thing that it points to.

提交回复
热议问题