rvalue

Assigning Rvalue returned from function to another Rvalue

。_饼干妹妹 提交于 2019-11-29 19:26:35
问题 class Test { public: int n1; }; Test func() { return Test(); } int main() { func() = Test(); } This doesn't make sense to me. How and why is this allowed? Is it undefined behavior? If a function returns an rvalue, then how is it possible to set an rvalue to another rvalue? If I tried this with any primitive types, it would give me an error like I expect. I know that lvalues are a place in memory, so is the function creating a temporary lvalue (rvalue?) and assigning it to another lvalue? Can

rvalue on the left side

a 夏天 提交于 2019-11-29 18:54:32
问题 Why is this code compiling? I thought that rvalues returned by ctor are not located in memory and therefore can't be used as lvalues. #include <iostream> #include <vector> class Y { public : explicit Y(size_t num = 0) : m_resource {std::vector<int>(num)} { } std::vector<int> m_resource; }; int main(int argc, const char * argv[]) { Y(1) = Y(0); // WHAT?!? return 0; } 回答1: The synthesized assignment operator is declared as one of these (if it can be synthesized and isn't declared as deleted)

literal and rvalue reference

冷暖自知 提交于 2019-11-29 14:41:12
void test(int && val) { val=4; } void main() { test(1); std::cin.ignore(); } Is a int is created when test is called or by default in c++ literals are int type? Nawaz Note that your code would compile only with C++11 compiler. When you pass an integral literal, which is by default of int type, unless you write 1L , a temporary object of type int is created which is bound to the parameter of the function. It's like the first from the following initializations: int && x = 1; //ok. valid in C++11 only. int & y = 1; //error, both in C++03, and C++11 const int & z = 1; //ok, both in C++03, and C+

rvalues and temporary objects in the FCD

与世无争的帅哥 提交于 2019-11-29 11:03:52
It took me quite some time to understand the difference between an rvalue and a temporary object. But now the final committee draft states on page 75: An rvalue [...] is an xvalue, a temporary object or subobject thereof, or a value that is not associated with an object. I can't believe my eyes. This must be an error, right? To clarify, here is how I understand the terms: #include <string> void foo(std::string&& str) { std::cout << str << std::endl; } int main() { foo(std::string("hello")); } In this program, there are two expressions that denote the same temporary object : the prvalue std:

Why don't rvalues have an address?

陌路散爱 提交于 2019-11-29 10:49:41
Why don't rvalues have a memory address? Are they not loaded into the RAM when the program executes or does it refer to the values stored in processor registers? Your question ("Why don't rvalues have a memory address?") is a bit confused. An rvalue is a kind of expression. Expressions don't have addresses: objects have addresses. It would be more correct to ask "why can one not apply the address-of operator to an rvalue expression?" The answer to that is rather simple: you can only take the address of an object and not all rvalue expressions refer to objects (for example, the expression 42

Exact difference between rvalue and lvalue

笑着哭i 提交于 2019-11-29 10:25:15
问题 While I was reading http://thbecker.net/articles/rvalue_references/section_01.html, I got following snippiest. // lvalues: // int i = 42; i = 43; // ok, i is an lvalue int& foo(); foo() = 42; // ok, foo() is an lvalue int* p1 = &foo(); // ok, foo() is an lvalue // rvalues: // int foobar(); int j = 0; j = foobar(); // ok, foobar() is an rvalue int* p2 = &foobar(); // error, cannot take the address of an rvalue j = 42; // ok, 42 is an rvalue Why int* p2 = &foobar(); is error statement, while

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

断了今生、忘了曾经 提交于 2019-11-29 04:53:54
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't compile without the std::move } }; int main() { unique_ptr<Shape> ups(new Shape); View v; v.add_shape

Difference between “return-by-rvalue-ref” & “return-by-value” when you return using std::move?

我与影子孤独终老i 提交于 2019-11-29 04:08:51
Considering the following code: #include <iostream> using namespace std; struct I { I(I&& rv) { cout << "I::mvcotr" << endl; } }; struct C { I i; I&& foo() { return move(i) }; } }; int main() { C c; I i = c.foo(); } C contains I. And C::foo() allows you to move I out of C. What is the difference between the member function used above: I&& foo() { return move(i) }; // return rvalue ref and the following replacement member function: I foo() { return move(i) }; // return by value To me, they seem to do the same thing: I i = c.foo(); leads to a call to I::I(I&&); . What consequences will there be

Why does std::move take a forward reference?

元气小坏坏 提交于 2019-11-29 04:06:28
The implementation of std::move basically looks like this: template<typename T> typename std::remove_reference<T>::type&& move(T&& t) { return static_cast<typename std::remove_reference<T>::type&&>(t); } Note that the parameter of std::move is a universal reference (also known as a forwarding reference, but we're not forwarding here). That is, you can std::move both lvalues and rvalues: std::string a, b, c; // ... foo(std::move(a)); // fine, a is an lvalue foo(std::move(b + c)); // nonsense, b + c is already an rvalue But since the whole point of std::move is to cast to an rvalue, why are we

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

拟墨画扇 提交于 2019-11-29 02:13:40
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 ‘void passByRef(A&)’ It says that I can't pass an rvalue as an argument of a non-const reference, but