lvalue

Why is taking the address of a temporary illegal?

你说的曾经没有我的故事 提交于 2019-11-28 09:09:08
I know that the code written below is illegal void doSomething(std::string *s){} int main() { doSomething(&std::string("Hello World")); return 0; } The reason is that we are not allowed to take the address of a temporary object. But my question is WHY? Let us consider the following code class empty{}; int main() { empty x = empty(); //most compilers would elide the temporary return 0; } The accepted answer here mentions "usually the compiler consider the temporary and the copy constructed as two objects that are located in the exact same location of memory and avoid the copy." According to the

lvalue binding to rvalue reference

旧街凉风 提交于 2019-11-28 07:54:55
问题 I am trying to understand how lvalues bind to rvalue references. Consider this code: #include <iostream> template<typename T> void f(T&& x) { std::cout << x; } void g(int&& x) { std::cout << x; } int main() { int x = 4; f(x); g(x); return 0; } While the call to f() is fine, the call to g() gives a compile-time error. Does this kind of binding work only for templates? Why? Can we somehow do it without templates? 回答1: Since T is a template argument, T&& becomes a forwarding-reference . Due to

Function that accepts both lvalue and rvalue arguments

若如初见. 提交于 2019-11-28 07:13:51
Is there a way to write a function in C++ that accepts both lvalue and rvalue arguments, without making it a template? For example, suppose I write a function print_stream that reads from an istream and prints the data that was read to the screen, or something. I think it's reasonable to call print_stream like this: fstream file{"filename"}; print_stream(file); as well as like this: print_stream(fstream{"filename"}); But how do I declare print_stream so that both uses work? If I declare it as void print_stream(istream& is); then the second use won't compile because an rvalue will not bind to a

error: invalid initialization of non-const reference of type ‘bool&’ from an rvalue of type ‘std::vector<bool>::reference {aka std::_Bit_reference}’

て烟熏妆下的殇ゞ 提交于 2019-11-28 03:43:00
问题 Why do I get the error: invalid initialization of non-const reference of type ‘bool&’ from an rvalue of type ‘std::vector::reference {aka std::_Bit_reference}’? vector<vector<bool>> vis; bool& visited(int x, int y) { return vis[x][y]; //error } As far as I know operator[] in vector returns reference, so it should be an lvalue, but it doesn't work. What should I do to make it work? 回答1: That's because std::vector< bool > is not what it looks like. There's a specialization for std::vector with

C++: is return value a L-value?

旧时模样 提交于 2019-11-28 03:38:30
Consider this code: struct foo { int a; }; foo q() { foo f; f.a =4; return f;} int main() { foo i; i.a = 5; q() = i; } No compiler complains about it, even Clang. Why q() = ... line is correct? No, the return value of a function is an l-value if and only if it is a reference (C++03). (5.2.2 [expr.call] / 10) If the type returned were a basic type then this would be a compile error. (5.17 [expr.ass] / 1) The reason that this works is that you are allowed to call member functions (even non- const member functions) on r-values of class type and the assignment of foo is an implementation defined

lvalue required

谁说胖子不能爱 提交于 2019-11-28 03:22:13
问题 what does the error message "Lvalue required" actually mean? 回答1: An lvalue is something that can appear on the left side of an assignment, in other words 'something that can be assigned' So, look for an assignment where the left hand side isn't 'assignable', for example, something as simple as this might trigger such an error if (0 = foo) { } Here you've got an attempt to assign to a constant because of accidentally using = rather than == See also often used seldom defined terms: lvalue

C++0x const RValue reference as function parameter

岁酱吖の 提交于 2019-11-28 00:38:47
I am trying to understand why someone would write a function that takes a const rvalue reference . In the code example below what purpose is the const rvalue reference function (returning "3"). And why does overload resolution preference the const Rvalue above the const LValue reference function (returning "2"). #include <string> #include <vector> #include <iostream> std::vector<std::string> createVector() { return std::vector<std::string>(); } //takes movable rvalue void func(std::vector<std::string> &&p) { std::cout << "1"; } //takes const lvalue void func(const std::vector<std::string> &p)

lvalue required as increment operand error

筅森魡賤 提交于 2019-11-28 00:09:19
#include <stdio.h> int main() { int i = 10; printf("%d\n", ++(-i)); // <-- Error Here } What is wrong with ++(-i) ? Please clarify. -i generates a temporary and you can't apply ++ on a temporary(generated as a result of an rvalue expression). Pre increment ++ requires its operand to be an lvalue, -i isn't an lvalue so you get the error. The ++ operator increments a variable. (Or, to be more precise, an lvalue —something that can appear on the l eft side of an assignment expression) (-i) isn't a variable, so it doesn't make sense to increment it. Lee Louviere You can't increment a temporary

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

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,