Are all temporaries rvalues in C++?

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

    Prasoon Saurav already linked a very good clc++ thread. In there, James Kanze explains why the question doesn't really make sense. It boils down to:

    • rvalue-ness is a (boolean) property of expressions - each expression is either an lvalue or an rvalue
    • temporaries are not expressions

    For that reason, the question doesn't make sense.

    A good example is the following code:

    int main() {
      const int& ri = 4;
      std::cout << ri << std::endl; 
    }
    

    The temporary int with value 4 is not an expression. The expression ri that's printed is not a temporary. It's an lvalue, and refers to a temporary.

提交回复
热议问题