rvalue

Confusion about an error: lvalue required as unary '&' operand [duplicate]

浪子不回头ぞ 提交于 2019-12-01 03:34:24
问题 This question already has answers here : Not able to understand error condition wrt lvalues (3 answers) Closed 5 months ago . I have been trying to understand pointer concepts by writing simple code, and I got an error problem, and it seems like I couldn't solve it or understand it. #include <stdio.h> int *foo(void); int main(void) { printf("%d\n", *foo()); return 0; } int *foo(void) { static int num = 1; ++num; return &(++num); } Here is the error message. error: lvalue required as unary ‘&’

Is a data member of a temporary object an xvalue in C++11?

别来无恙 提交于 2019-11-30 22:37:16
问题 #include <vector> using namespace std; struct A { vector<int> coll; }; void f(const vector<int>&){} void f(vector<int>&&){} int main() { f(A().coll); // Is "A().coll" an xvalue? } Does C++11 guarantee f(A().coll) will call void f(vector<int>&&) ? 回答1: Yes. C++14 standard, §5.2.5/4.2, given E1.E2 : If E2 is a non-static data member and the type of E1 is “ cq1 vq1 X ”, and the type of E2 is “ cq2 vq2 T ”, the expression designates the named member of the object designated by the first

structured bindings with std::minmax and rvalues

孤人 提交于 2019-11-30 13:48:20
问题 I ran into a rather subtle bug when using std::minmax with structured bindings. It appears that passed rvalues will not always be copied as one might expect. Originally I was using a T operator[]() const on a custom container, but it seems to be the same with a literal integer. #include <algorithm> #include <cstdio> #include <tuple> int main() { auto [amin, amax] = std::minmax(3, 6); printf("%d,%d\n", amin, amax); // undefined,undefined int bmin, bmax; std::tie(bmin, bmax) = std::minmax(3, 6)

PODs, non-PODs, rvalue and lvalues

旧街凉风 提交于 2019-11-30 12:38:22
问题 Could anyone explain the details in terms of rvalues, lvalues, PODs, and non-PODs the reason why the first expression marked below is not ok while the second expression marked below is ok? In my understanding both int() and A() should be rvalues, no? struct A {}; int main() { int i; A a; int() = i; //Not OK (error). A() = a; //OK. return 0; } 回答1: Rvalues are what you get from expressions (a useful simplification taken from the C standard, but not worded in C++ standardese). Lvalues are

Address of a temporary in Go?

守給你的承諾、 提交于 2019-11-30 11:46:52
问题 What's the cleanest way to handle a case such as this: func a() string { /* doesn't matter */ } b *string = &a() This generates the error: cannot take the address of a() My understanding is that Go automatically promotes a local variable to the heap if its address is taken. Here it's clear that the address of the return value is to be taken. What's an idiomatic way to handle this? 回答1: The address operator returns a pointer to something having a "home", e.g. a variable. The value of the

C++11: Why is assigning rvalues allowed?

送分小仙女□ 提交于 2019-11-30 10:02:16
From what I understand the reason why it is dangerous to return rvalues references to from functions is due to the following code: T&& f(T&& x) { do_something_to_T(x); return static_cast<T&&>(x); } T f(const T& x) { T x2 = x; do_something_to_T(x2); return x2; } T&& y = f(T()); This leaves y as an undefined dangling reference. However, I don't understand why the above code even compiles? Is there ever a legitimate reason to assign a rvalue reference to another rvalue reference? Aren't rvalues suppose to be, roughly speaking, "temporaries", i.e. going to be made invalid at the end of the

structured bindings with std::minmax and rvalues

回眸只為那壹抹淺笑 提交于 2019-11-30 08:14:46
I ran into a rather subtle bug when using std::minmax with structured bindings. It appears that passed rvalues will not always be copied as one might expect. Originally I was using a T operator[]() const on a custom container, but it seems to be the same with a literal integer. #include <algorithm> #include <cstdio> #include <tuple> int main() { auto [amin, amax] = std::minmax(3, 6); printf("%d,%d\n", amin, amax); // undefined,undefined int bmin, bmax; std::tie(bmin, bmax) = std::minmax(3, 6); printf("%d,%d\n", bmin, bmax); // 3,6 } Using GCC 8.1.1 with -O1 -Wuninitialized will result in 0,0

Const reference and lvalue [duplicate]

主宰稳场 提交于 2019-11-30 02:08:07
This question already has an answer here: Literal initialization for const references 3 answers 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? 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] Then, the resulting value is placed in a temporary variable of type T. [3] Finally, this temporary variable is used as the value

Address of a temporary in Go?

独自空忆成欢 提交于 2019-11-30 01:17:37
What's the cleanest way to handle a case such as this: func a() string { /* doesn't matter */ } b *string = &a() This generates the error: cannot take the address of a() My understanding is that Go automatically promotes a local variable to the heap if its address is taken. Here it's clear that the address of the return value is to be taken. What's an idiomatic way to handle this? zzzz The address operator returns a pointer to something having a "home", e.g. a variable. The value of the expression in your code is "homeless". if you really need a *string, you'll have to do it in 2 steps: tmp :=

Why are C++0x rvalue reference not the default?

点点圈 提交于 2019-11-29 22:24:52
One of the cool new features of the upcoming C++ standard, C++0x, are "rvalue references." An rvalue reference is similar to an lvalue (normal) reference, except that it can be bound to a temporary value (normally, a temporary can only be bound to a const reference): void FunctionWithLValueRef(int& a) {...} void FunctionWithRValueRef(int&& a) {...} int main() { FunctionWithLValueRef(5); // error, 5 is a temporary FunctionWithRValueRef(5); // okay } So, why did they invent a whole new type, instead of just removing the restrictions on normal references to allow them to be bound to temporaries?