rvalue

Does `const &&` bind to all prvalues (and xvalues)?

坚强是说给别人听的谎言 提交于 2019-12-19 17:34:10
问题 The C++ standard defines the following functions deleted; template <class T> void ref(const T&&) = delete; template <class T> void cref(const T&&) = delete; This is to aid in ensuring that the functions are not misused by disallowing them from binding to temporary values (rvalues). Does const && bind to all rvalues, specifically prvalues? Would const && bind to all "moved objects" (xvalues; basically something returned from std::move or similar)? I can reason that it should, but I don't have

Does `const &&` bind to all prvalues (and xvalues)?

做~自己de王妃 提交于 2019-12-19 17:32:21
问题 The C++ standard defines the following functions deleted; template <class T> void ref(const T&&) = delete; template <class T> void cref(const T&&) = delete; This is to aid in ensuring that the functions are not misused by disallowing them from binding to temporary values (rvalues). Does const && bind to all rvalues, specifically prvalues? Would const && bind to all "moved objects" (xvalues; basically something returned from std::move or similar)? I can reason that it should, but I don't have

Why isn't this rvalue promoted to an lvalue as specified in the reference?

℡╲_俬逩灬. 提交于 2019-12-18 08:16:40
问题 The Rust Reference says: The left operand of an assignment or compound-assignment expression is an lvalue context, as is the single operand of a unary borrow. [...] When an rvalue is used in an lvalue context, a temporary un-named lvalue is created and used instead. This rvalue promotion obviously works with borrowing: let ref_to_i32 = &27; // a temporary i32 variable with value 27 is created But it doesn't seem to work in an assignment (although the reference speaks about all lvalue contexts

Why isn't this rvalue promoted to an lvalue as specified in the reference?

冷暖自知 提交于 2019-12-18 08:16:07
问题 The Rust Reference says: The left operand of an assignment or compound-assignment expression is an lvalue context, as is the single operand of a unary borrow. [...] When an rvalue is used in an lvalue context, a temporary un-named lvalue is created and used instead. This rvalue promotion obviously works with borrowing: let ref_to_i32 = &27; // a temporary i32 variable with value 27 is created But it doesn't seem to work in an assignment (although the reference speaks about all lvalue contexts

is rvalue passed as parameter treated as lvalue inside the function?

北战南征 提交于 2019-12-18 04:19:20
问题 I have a View and a Shape class where the View "owns" its Shape objects. I am implementing this as a vector of unique_ptr. In the function View::add_shape(std::unique_ptr&& shape), I still need to use std::move on the rvalue parameter to make it compile. Why? ( using GCC 4.8 ) #include <memory> #include <vector> using namespace std; class Shape { }; class View { vector<unique_ptr<Shape>> m_shapes; public: void add_shape(unique_ptr<Shape>&& shape) { m_shapes.push_back(std::move(shape));// won

I think I may have come up with an example of rvalue of array type

亡梦爱人 提交于 2019-12-18 03:35:36
问题 C++03 §4.2 N°1: An lvalue or rvalue of type “array of N T” or “array of unknown bound of T” can be converted to an rvalue of type “pointer to T.” The result is a pointer to the first element of the array. What has been confusing in this statement for a long time for me was that I didn't quite understand what an rvalue of array type would mean. That is, I couldn't come up with an expression whose type were an array and the result were an rvalue. I read this thread, which basically asks the

Why pre-increment operator gives rvalue in C?

百般思念 提交于 2019-12-17 22:57:49
问题 In C++, pre-increment operator gives lvalue because incremented object itself is returned, not a copy. But in C, it gives rvalue. Why? 回答1: C doesn't have references. In C++ ++i returns a reference to i (lvalue) whereas in C it returns a copy(incremented). C99 6.5.3.1/2 The value of the operand of the prefix ++ operator is incremented. The result is the new value of the operand after incrementation . The expression ++Eis equivalent to (E+=1). ‘‘value of an expression’’ <=> rvalue However for

Const reference and lvalue [duplicate]

雨燕双飞 提交于 2019-12-17 22:43:43
问题 This question already has answers here : Literal initialization for const references (3 answers) Closed 5 years ago . We cannot write int& ref = 40 because we need lvalue on right side. But we can write const int& ref = 40 . Why is this possible? 40 is rvalue instead lvalue I know that this is an exception but why? 回答1: As Stroustrup says: The initializer for a const T& need not be an lvalue or even of type T. In such cases: [1] First, implicit type conversion to T is applied if necessary. [2

Regarding lvalue-to-rvalue conversion, when is it required?

大兔子大兔子 提交于 2019-12-17 20:22:21
问题 I've been reading quite many on the Internet and it seems that many people mentioned the following rules (but i couldn't find it in the standard), The addition operator + (and all other binary operators) requires both operands to be rvalue, and the result is rvalue. And so on.. I checked the C++ standard, and it clearly states that (clause 3.10/2), Whenever a glvalue appears in a context where a prvalue is expected, the glvalue is converted to a prvalue (clause 5/9), Whenever a glvalue

C++ function returns a rvalue, but that can be assigned a new value?

落花浮王杯 提交于 2019-12-17 18:57:59
问题 The code is as follows: #include <iostream> using namespace std; class A { }; A rtByValue() { return A(); } void passByRef(A &aRef) { // do nothing } int main() { A aa; rtByValue() = aa; // compile without errors passByRef(rtByValue()); // compile with error return 0; } The g++ compiler gives the following error: d.cpp: In function ‘int main()’: d.cpp:19:23: error: invalid initialization of non-const reference of type ‘A&’ from an rvalue of type ‘A’ d.cpp:12:6: error: in passing argument 1 of