rvalue

Binding temporary to a lvalue reference

南楼画角 提交于 2019-11-27 23:50:43
I have the following code string three() { return "three"; } void mutate(string& ref) { } int main() { mutate(three()); return 0; } You can see I am passing three() to mutate method. This code compiles well. My understanding is, temporaries can't be assigned to non-const references. If yes, how this program is compiling? Any thoughts? Edit: Compilers tried : VS 2008 and VS2010 Beta It used to compile in VC6 compiler, so I guess to maintain backward comptibility VS2008 is supporting this non-standard extension. Try with /Za (disable language extension) flag, you should get an error then. It is

Should implicitly generated assignment operators be & ref-qualified?

北慕城南 提交于 2019-11-27 22:23:19
The following code compiles without problem on gcc 4.8.1: #include <utility> struct foo { }; int main() { foo bar; foo() = bar; foo() = std::move( bar ); } It seems the implicitly generated assignment operators for foo are not & ref-qualified and so can be invoked on rvalues. Is this correct according to the standard? If so, what reason is there for not requiring implicitly generated assignment operators to be & ref-qualified? Why doesn't the standard require the following to be generated? struct foo { foo & operator=( foo const & ) &; foo & operator=( foo && ) &; }; Well, there are certain

Is a member of an rvalue structure an rvalue or lvalue?

余生颓废 提交于 2019-11-27 21:08:30
A function call returning a structure is an rvalue expression, but what about its members? This piece of code works well with my g++ compiler, but gcc gives a error saying "lvalue required as left operand of assignment": struct A { int v; }; struct A fun() { struct A tmp; return tmp; } int main() { fun().v = 1; } gcc treats fun().v as rvalue, and I can understand that. But g++ doesn't think the assignment expression is wrong. Does that mean fun1().v is lvalue in C++? Now the problem is, I searched the C++98/03 standard, finding nothing telling about whether fun().v is lvalue or rvalue. So,

How to determine programmatically if an expression is rvalue or lvalue in C++?

▼魔方 西西 提交于 2019-11-27 20:26:38
问题 What's the best way to determine if an expression is a rvalue or lvalue in C++? Probably, this is not useful in practice but since I am learning rvalues and lvalues I thought it would be nice to have a function is_lvalue which returns true if the expression passed in input is a lvalue and false otherwise. Example: std::string a("Hello"); is_lvalue(std::string()); // false is_lvalue(a); // true 回答1: Most of the work is already done for you by the stdlib, you just need a function wrapper:

Are all temporaries rvalues in C++?

北城以北 提交于 2019-11-27 19:39:28
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? If no, can anyone provide me an example where temporary produced in the code is an lvalue ? No. The C++ language specification never makes such a straightforward assertion as the one you are asking about. It doesn't say anywhere in the language standard that "all temporary objects are rvalues". Moreover, the question itself is a bit of misnomer, since the property of being an rvalue in the C++ language is not a property of an

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

大憨熊 提交于 2019-11-27 18:18:48
问题 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

Why does std::move take a forward reference?

我与影子孤独终老i 提交于 2019-11-27 18:05:47
问题 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,

Why are rvalues references variables not rvalue?

痴心易碎 提交于 2019-11-27 15:09:56
Let's say I have two overloads of a function f . f(T&&) and f(T&) . Then in the body of g : g(T&& t) { f(t);} the overload f(T&) will be called because t is considered an lvalue. This is very surprising to me. How a function with signature f(T&&) can not match a call with type T&& ? What suprises me even more is that a call f(static_cast<T&&>(t)) would actually call the rvalue overload f(T&&) . What are the C++ rules that make this possible? Is T&& more than a type? The things that are automatically treated as rvalues are things without names, and things that (very shortly) will not have a

How can a returned object be assignable?

冷暖自知 提交于 2019-11-27 13:46:21
问题 In Effective C++, Item 3, Scott Meyers suggests overloading operator* for a class named Rational : class Rational { ... }; const Rational operator*(const Rational& lhs, const Rational& rhs); The reason for the return value being const -qualified is explained in the following line: if it were not const , programmers could write code such as: (a * b) = c; or, more probably: if (a*b = c) Fair enough. Now I’m confused as I thought that the return value of a function, here operator*, was a rvalue,

Why “universal references” have the same syntax as rvalue references?

こ雲淡風輕ζ 提交于 2019-11-27 11:28:20
I just made some research about those (quite) new features and I wonder why C++ Committee decided to introduce the same syntax for both of them? It seems that developers unnecessary have to waste some time to understand how it works, and one solution lets to think about further problems. In my case it started from problem which can be simplified to this: #include <iostream> template <typename T> void f(T& a) { std::cout << "f(T& a) for lvalues\n"; } template <typename T> void f(T&& a) { std::cout << "f(T&& a) for rvalues\n"; } int main() { int a; f(a); f(int()); return 0; } I compiled it