C++ 11

2014-09-28 5 views
-4
#include <iostream> 
using namespace std; 

int&& move(int&& t) { 
    return static_cast<int &&>(t); 
} 

int main() { 
    move(5) = 10;//why error? 

    return 0; 
} 

오류C++ 11

prog.cpp:9:13: error: using xvalue (rvalue reference) as lvalue 

Here's the complete sample

나는 이해할 수 없다. 함수가 rvalue 참조를 반환하지 않습니까? 나는 표준, lvalue의, xvalue들과 prvalue의 해석 방법에서

+0

당신은 할 수 없습니다 "이동"정수, 당신이 그것을에 r 값 참조가 경우에도 마찬가지입니다. 그리고 정수 리터럴에 *를 * 지정할 수 없습니다. – Cameron

+1

오류 메시지의 모든 용어를 연구하는 것이 좋습니다. –

+0

오류 : xvalue (값 참조)를 lvalue로 사용 – user2178911

답변

2

§ 3.10

는 상호 배타적입니다. 5 같은

Every expression belongs to exactly one of the fundamental classifications in this taxonomy: lvalue, xvalue, or prvalue.

리터럴 예는 prvalue이다. 과제가 실패하는 이유에 관해서는

[ Example: The result of calling a function whose return type is an rvalue reference is an xvalue. — end example ]

은 다음을 참조하십시오 :

5 An lvalue for an object is necessary in order to modify the object except that an rvalue of class type can also be used to modify its referent under certain circumstances. [ Example: a member function called for aobject (9.3) can modify the object. — end example ]

cppreference's page on assigment operators가 더 분명 언어로이 문제를두고

— A prvalue (“pure” rvalue) is an rvalue that is not an xvalue. [ Example: The result of calling a function whose return type is not a reference is a prvalue. The value of a literal such as 12, 7.3e5, or true is also a prvalue. — end example ]

그리고 당신의 "이동"기능에

, 당신은이 xvalue 때문에 얻을 . 그래서 마지막으로

The direct assignment operator expects a modifiable lvalue as its left operand and an rvalue expression or a braced-init-list (since C++11) as its right operand, and returns an lvalue identifying the left operand after modification.

:

 move(5) =  10 
xvalue^ prvalue^

참조 : Value categories