473,386 Members | 1,835 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Get lvalue through rvalue

200 100+
Hello everyone,


I do not know how in the following code, rvalue -- return of X(), could result in a lvalue finally and binded to a non-const reference input parameter of function f.

Any ideas?

Expand|Select|Wrap|Line Numbers
  1. struct X {
  2.  
  3.  
  4. };
  5.  
  6. void f (X& x) {}
  7.  
  8. int main()
  9. {
  10.     f (X() = X());
  11.  
  12.     return 0;
  13. }
  14.  

thanks in advance,
George
Dec 17 '07 #1
4 1020
weaknessforcats
9,208 Expert Mod 8TB
This is silly code.

X() creates an X object by calling the default constructor. Therefore:

X() = X()

Creates two X objects and assigns the one on the right to the one on the left. That left object is bound as the non-const reference used on the call to f().

The code above uis the same as:
Expand|Select|Wrap|Line Numbers
  1. X obj1;
  2. X obj2;
  3. obj1 = obj2;
  4. f(obj1);
  5.  
Dec 17 '07 #2
George2
200 100+
Thanks weaknessforcats,


Do you think my original sample breaks the rule that non-const reference can not binded to a rvalue (temporary object return by X())?

This is silly code.

X() creates an X object by calling the default constructor. Therefore:

X() = X()

Creates two X objects and assigns the one on the right to the one on the left. That left object is bound as the non-const reference used on the call to f().

The code above uis the same as:
Expand|Select|Wrap|Line Numbers
  1. X obj1;
  2. X obj2;
  3. obj1 = obj2;
  4. f(obj1);
  5.  

regards,
George
Dec 19 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
Do you think my original sample breaks the rule that non-const reference can not binded to a rvalue (temporary object return by X())?
Where did you hear this??

Syntatically, you can always return a non-const pointer or reference to a local object. However, the the pointer or reference will become garbage once the temporary object disappears. On my compiler doing this generates a warning that I am doing is a bad thing.
Dec 19 '07 #4
George2
200 100+
Thanks for your advice, weaknessforcats!


Where did you hear this??

Syntatically, you can always return a non-const pointer or reference to a local object. However, the the pointer or reference will become garbage once the temporary object disappears. On my compiler doing this generates a warning that I am doing is a bad thing.

regards,
George
Dec 20 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: Hongzheng Wang | last post by:
In K&R, they said: An object is a named region of storage; an lvalue is an expression refer to an object. How about these concept in C++? `The C++ Programming Language' has a similar...
9
by: Steven T. Hatton | last post by:
This is from the draft of the previous version of the Standard: http://www.kuzbass.ru:8086/docs/isocpp/expr.html 2- A literal is a primary expression. Its type depends on its form...
24
by: Romeo Colacitti | last post by:
Hi, Does anyone here have a strong understanding for the meanings of the terms "lvalue" and "rvalue" as it pertains to C, objects, and different contexts? If so please share. I've been...
9
by: Kavya | last post by:
These were the questions asked to me by my friend 1. Operator which may require an lvalue operand, yet yield an rvalue. 2. Operator which may require an rvalue operand, yet yield an lvalue. ...
14
by: nobrow | last post by:
Yes I know what lvalue means, but what I want to ask you guys about is what are all valid lvalues ... a *a a *(a + 1) .... What else?
6
by: Lighter | last post by:
How to read "The lvalue-to-rvalue, array-to-pointer, and function-to- pointer standard conversionsare not applied to the left expressions"? In 5.18 Comma operator of the C++ standard, there is a...
6
by: Yarco | last post by:
I've alway thought lvalue means Left Value and rvalue means Right Value before i've read someone's article. It is said "lvalue = location value" and "rvalue = read value". Which one is right, then?
9
by: usao | last post by:
Does anyone have an example of how to create a class which describes and array, such that I can use the subscript operator on both the left and right side of an assignment statement? This is as...
2
by: Chad | last post by:
The following question actually stems from an old Chris Torek post. And I quote from the following old CLC url ...
2
by: Chad | last post by:
I have the folllowing #include <stdio.h> int main(void) { int x = 1, y = 2; int *ip; ip = &x;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.