rvalue

Do rvalue references allow dangling references?

北城以北 提交于 2019-11-27 00:31:10
Consider the below. #include <string> using std::string; string middle_name () { return "Jaan"; } int main () { string&& danger = middle_name(); // ?! return 0; } This doesn't compute anything, but it compiles without error and demonstrates something that I find confusing: danger is a dangling reference, isn't it? Do rvalue references allow dangling references? If you meant "Is it possible to create dangling rvalue references" then the answer is yes. Your example, however, string middle_name () { return "Jaan"; } int main() { string&& nodanger = middle_name(); // OK. // The life-time of the

Binding temporary to a lvalue reference

为君一笑 提交于 2019-11-26 23:23:31
问题 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 回答1: It used to compile in VC6 compiler, so I guess to maintain backward comptibility VS2008 is supporting this

Isn't the const modifier here unnecessary? [duplicate]

最后都变了- 提交于 2019-11-26 22:18:59
This question already has an answer here: How can a returned object be assignable? 1 answer The " Effective C++ " Item 3 says "Use const whenever possible", and it gives an example like: const Rational operator*(const Rational& lhs, const Rational& rhs); to prevent clients from being able to commit atrocities like this: Rational a, b, c; ... (a * b) = c; // invoke operator= on the result of a*b! But isn't the non-reference return value of functions allready a rvalue ? So why bother doing this? TemplateRex The point is that for class types (but not for builtin types), a = b is just a shorthand

Should implicitly generated assignment operators be & ref-qualified?

笑着哭i 提交于 2019-11-26 20:58:23
问题 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?

Are all temporaries rvalues in C++?

冷暖自知 提交于 2019-11-26 19:57:11
问题 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 ? 回答1: 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

What is the value category of the operands of C++ operators when unspecified?

北城余情 提交于 2019-11-26 19:07:56
PREMISE: The C++11 Standard classifies expressions into three disjoint value categories : lvalues , xvalues , and prvalues (§ 3.10/1). An explanation of what value categories are is available for instance here . I am struggling to figure out what are the requirements of the different operators on the value category of their operands. Paragraph 3.10/1 specifies: [...] Every expression belongs to exactly one of the fundamental classifications in this taxonomy: lvalue, xvalue, or prvalue. This property of an expression is called its value category. [ Note: The discussion of each built-in operator

In C++, what categories (lvalue, rvalue, xvalue, etc.) can expressions that produce temporaries of class type fall into?

青春壹個敷衍的年華 提交于 2019-11-26 18:59:46
Here is some example code: #include <iostream> class Foo { public: explicit Foo(int x) : data(x) {}; Foo& operator++() { data += 1; return *this; } void *get_addr() { return (void*)this; } friend Foo operator + (const Foo& lhs, const Foo& rhs); friend std::ostream& operator << (std::ostream& os, const Foo& f); private: int data; }; std::ostream& operator << (std::ostream& os, const Foo& f) { return (os << f.data); } Foo operator + (const Foo& lhs, const Foo& rhs) { return Foo(lhs.data + rhs.data); } void bar(Foo& f) { std::cout << "bar(l-value ref)" << std::endl; } void bar(const Foo& f) { std

prolonging the lifetime of temporaries

假装没事ソ 提交于 2019-11-26 18:16:39
问题 What is the design rationale behind allowing this const Foo& a = function_returning_Foo_by_value(); but not this Foo& a = function_returning_Foo_by_value(); ? What could possible go wrong in the second line (which would not already go wrong in the first line)? 回答1: I'll answer your question... the other way around. Why did they allowed Foo const& foo = fooByValue(); to begin with ? It makes life (somewhat) easier, but introduces potential undefined behavior all over the place. Foo const&

Are literal strings and function return values lvalues or rvalues?

蓝咒 提交于 2019-11-26 16:08:27
Just wonder if a literal string is an lvalue or an rvalue. Are other literals (like int, float, char etc) lvalue or rvalue? Is the return value of a function an lvalue or rvalue? How do you tell the difference? string literals are lvalues, but you can't change them rvalue, but if it's a pointer and non-NULL, the object it points to is an lvalue The C standard recognizes the original terms stood for left and right as in L = R ; however, it says to think of lvalue as locator value , which roughly means you can get the address of an object and therefore that object has a location. (See 6.3.2.1 in

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

筅森魡賤 提交于 2019-11-26 15:35:22
问题 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: