rvalue

c++: function lvalue or rvalue

巧了我就是萌 提交于 2019-12-06 03:51:18
问题 I just started learning about rvalue references in c++11 by reading this page, but I got stuck into the very first page. Here is the code I took from that page. int& foo(); foo() = 42; // ok, foo() is an lvalue int* p1 = &foo(); // ok, foo() is an lvalue int foobar(); j = foobar(); // ok, foobar() is an rvalue int* p2 = &foobar(); // error, cannot take the address of an rvalue why is foo() an lvalue? is it because foo() returns int& which is basically an lvalue? why is foobar() an rvalue? is

Iterating through an rvalue container

谁说胖子不能爱 提交于 2019-12-06 03:26:35
Is the following code causing undefined behavior? std::map<int, vector<int>> foo() { return ... } BOOST_FOREACH(const int& i, foo()[42]) { std::cout << i << std::endl; } If undefined, What is the good way to fix it? What if I use c++11 range-for loop instead of BOOST_FOREACH? This is, unfortunately, most probably undefined behavior. The problem is that you have two levels here: std::map<...> is an r-value, its lifetime will be expanded until the end of the full-expression std::vector<int>& is an l-value reference (into an object), its lifetime is that of the object. The problem arises because

swap temporary tuples of references

不羁岁月 提交于 2019-12-06 01:43:25
问题 I'm writing a custom iterator that, when dereferenced returns a tuple of references. Since the tuple itself is ephemeral, I don't think I can return a reference from operator*(). I think my iterator makes sense semantically, since it has reference semantics, even though operator* returns a value. The issue is, when I try to call std::swap (or rather, when std::sort does), like below, I get errors because the swap expects l-values. Is there an easy fix to this problem? #include <vector> class

Why can an rvalue not bind to a non-const lvalue reference, other than the fact that writing to a temporary has no effect?

杀马特。学长 韩版系。学妹 提交于 2019-12-06 01:32:20
I have read the SO question here and understood this part of the answer: "But if you bind a temporary to a non-const reference, you can keep passing it around "forever" just to have your manipulation of the object disappear, because somewhere along the way you completely forgot this was a temporary." That is, in the following: #include <iostream> void modifyValue(int& rValue) { rValue++; } int main() { modifyValue(9899); return 0; } If an rvalue could bind to a non-const lvalue reference, then potentially many modifications could be done that would eventually be discarded (since an rvalue is

What is the scope of a literal value, and how does the compiler allocate memory to it?

假如想象 提交于 2019-12-06 00:55:00
int x = 12; 12 is said to be integer literal, and therefore can't be used in the LValue. How does the compiler allocate memory to a literals? What is the scope of a literals? Why can't we get its address with an &12 in its scope? OK Bad example in the question. But the question is still valid: Lets try: Foo getFoo() {return Foo();} int func() { getFoo().bar(); // Creates temporary. // before this comment it is also destroyed. // But it lives for the whole expression above // So you can call bar() on it. } int func2() { Foo const& tmp = getFoo(); // Creates temporary. // Does not die here as it

Does a Comparison Between an Lvalue and a Literal Invoke an Lvalue-to-Rvalue Conversion?

匆匆过客 提交于 2019-12-05 19:57:46
I asked this question: static_assert of const Variable And apparently it comes down to the question does a floating point lvalue get converted to an rvalue for the purposes of comparison? So in this code does an lvalue-to-rvalue conversion occur? const float foo = 13.0F; static_assert(foo > 0.0F, "foo must be greater than 0."); Yes, it is performed. Basically, it's all because 3.0 > 1.2 is a well-formed expression, that contains nothing but prvalues for operands. First, [expr]/9 states (emphasis mine) that Whenever a glvalue expression appears as an operand of an operator that expects a

Why can't I pass an rvalue std::stringstream by value to a function?

左心房为你撑大大i 提交于 2019-12-05 12:05:40
Why does this code not compile? #include <sstream> void f(std::stringstream) { } int main() { f(std::stringstream{}); } I get this error: error: use of deleted function ‘std::basic_stringstream<char>::basic_stringstream(const std::basic_stringstream<char>&)’ f(std::stringstream{}); ^ If I replace std::stringstream with another type that's noncopyable it works fine. Shouldn't this use stringstream 's move constructor? The missing move constructor for stringstream is a known missing feature in GCC's implementation of the C++ standard library. If I'm reading the comments on that report correctly,

How are rvalues in c++ stored in memory?

孤者浪人 提交于 2019-12-05 05:19:27
Trying to learn lvalues , rvalues and memory allocation for them. So with a lot of learning materials there is a bit of chaos. An rvalue is a value that needs to exist only in bounds of a expression where it was created (until C++11 at least). So it has an address and block of memory that it occupies. But by definition we cannot get an address of rvalue , because it is a temporary object in contrast to an lvalue . But even before C++11 we were able to get an address of rvalue by returning it from a function and saving it into a const reference type (uh, I guess not an address but a value). So,

Understanding the warning: binding r-value to l-value reference

左心房为你撑大大i 提交于 2019-12-04 22:02:34
问题 I want to pass a struct by reference so it won't be copied, but Resharper is giving the warning below: struct sometype { }; sometype foo() { sometype x; return x; } void bar() { sometype & a = foo();//Binding r-value to l-value reference is non-standard Microsoft C++ extension sometype && b = foo(); //ok } Questions: What's wrong with sometype & a = foo(); ? isn't the return value from foo() an lvalue and a is also an lvalue? Is sometype && b = foo(); actually rvalue reference? Does it "steal

C++ Operator Overloading [ ] for lvalue and rvalue

怎甘沉沦 提交于 2019-12-04 16:34:34
I made a class Array which holds an integer array. From the main function, I'm trying to get an element of the array in Array using [ ] as we do for arrays declared in main. I overloaded the operator [ ] as in the following code; the first function returns an lvalue and the second an rvalue (Constructors and other member functions are not shown.) #include <iostream> using namespace std; class Array { public: int& operator[] (const int index) { return a[index]; } int operator[] (const int index) const { return a[index]; } private: int* a; } However, when I try to call those two functions from