lvalue

On how to recognize Rvalue or Lvalue reference and if-it-has-a-name rule

ε祈祈猫儿з 提交于 2019-11-27 04:15:54
I was reading Thomas Becker's article on rvalue reference and their use. In there he defines what he calls if-it-has-a-name rule: Things that are declared as rvalue reference can be lvalues or rvalues. The distinguishing criterion is: if it has a name, then it is an lvalue. Otherwise, it is an rvalue. This sounds very reasonable to me. It also clearly identifies the rvalueness of an rvalue reference. My questions are: Do you agree with this rule? If not, can you give an example where this rule can be violated? If there are no violations of this rule. Can we use this rule to define rvalueness

“l-value required” error

早过忘川 提交于 2019-11-27 03:41:49
问题 When do we get "l-value required" error...while compiling C++ program???(i am using VC++ ) 回答1: An "lvalue" is a value that can be the target of an assignment. The "l" stands for "left", as in the left hand side of the equals sign. An rvalue is the right hand value and produces a value, and cannot be assigned to directly. If you are getting "lvalue required" you have an expression that produces an rvalue when an lvalue is required. For example, a constant is an rvalue but not an lvalue. So: 1

Why is taking the address of a temporary illegal?

半城伤御伤魂 提交于 2019-11-27 02:46:06
问题 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

Function that accepts both lvalue and rvalue arguments

寵の児 提交于 2019-11-27 01:46:21
问题 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

what is return type of assignment operator?

China☆狼群 提交于 2019-11-27 01:01:36
I am just starting C++. I am a bit confused about the return type of assignment and dereference operator. I am following the book C++ Primer. At various occasions, the author says that the return type of assignment operator is reference to the type of left hand operand but later on, he says that the return type is the type of the left hand operand. I have referred C++11 Standard Sec. 5.17, where the return type is described as "lvalue referring to left hand operand". Similarly, I can't figure out whether dereference returns the pointed-to object or the reference to the object. Are these

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

Why does an lvalue cast work?

此生再无相见时 提交于 2019-11-26 22:09:26
问题 I saw this kind of cast for the first time today, and I'm curious as to why this works. I thought casting in this manner would assign to the temporary, and not the class member. Using VC2010. class A { public: A() : m_value(1.f) { ((float)m_value) = 10.f; } const float m_value; }; 回答1: It shouldn't work. An explicit type conversion to float with cast notation will be a prvalue (§5.4): The result of the expression (T) cast-expression is of type T . The result is an lvalue if T is an lvalue

Casting a pointer does not produce an lvalue. Why?

佐手、 提交于 2019-11-26 17:49:05
问题 After posting one of my most controversial answers here, I dare to ask a few questions and eventually fill some gaps in my knowledge. Why isn't an expression of the kind ((type_t *) x) considered a valid lvalue, assuming that x itself is a pointer and an lvalue, not just some expression? I know many will say "the standard disallows it", but from a logical standpoint it seems reasonable. What is the reason that the standard disallows it? After all, any two pointers are of the same size and the

Is the result of a cast an rvalue?

六眼飞鱼酱① 提交于 2019-11-26 17:47:57
问题 Let int a = 0; Then is (int)a an rvalue in standard C++? Different compilers show different results for this code: #include <iostream> using namespace std; void f(int& x) { cout << "l value" << endl; } void f(int&& x) { cout << "r value" << endl; } int main() { int a = 0; f((int)a); } compilers with different results: 1) http://cpp.sh/2r6 2) http://webcompiler.cloudapp.net/ 回答1: The should be an rvalue but webcompiler is running Visual Studio and Visual Studio has an extension which allows

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