lvalue rvalue discussion
-
Thanks NishantB++, Do you agree "in C++, string literals are l-values." in thread http://www.velocityreviews.com/forums/t279868-what-is-lvalue.html[^]? Why? regards, George
George_George wrote:
Do you agree "in C++, string literals are l-values." in thread http://www.velocityreviews.com/forums/t279868-what-is-lvalue.html\[^\]? Why?
No, that would mean something like:
"abc" = "123";
or
"myohmy" = 17;
would be legal.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
George_George wrote:
Do you agree "in C++, string literals are l-values." in thread http://www.velocityreviews.com/forums/t279868-what-is-lvalue.html\[^\]? Why?
No, that would mean something like:
"abc" = "123";
or
"myohmy" = 17;
would be legal.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
Hi DavidCrow, Are you sure it can compile?
#include <string>
int main()
{
"abc" = "123";"myohmy" = 17; return 0;
}
Compile error,
1>d:\visual studio 2008\projects\test_string2\test_string2\main.cpp(5) : error C2106: '=' : left operand must be l-value
1>d:\visual studio 2008\projects\test_string2\test_string2\main.cpp(7) : error C2440: '=' : cannot convert from 'int' to 'const char [7]'regards, George
-
manish.patel wrote:
t says that if we write x = 10; then x will be l-value(left side value) In c++, we can not give number as variable name, It always be a string only. So in C++, string literals are l-values.
Your argument is wrong. In the expression
x = 10;
the termx
is an identifier, NOT a string literal (a string literal is, for instance,"foo"
). See [^]. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain ClarkeThanks CPallini, What do you think of whether string literal is lvalue or rvalue? I think it is only rvalue, do you agree? regards, George
-
Thanks CPallini, What do you think of whether string literal is lvalue or rvalue? I think it is only rvalue, do you agree? regards, George
George_George wrote:
What do you think of whether string literal is lvalue or rvalue? I think it is only rvalue, do you agree?
Yes, of course. Did you ever see a string literal on the left of an assignment expression? :-D
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
Hi DavidCrow, Are you sure it can compile?
#include <string>
int main()
{
"abc" = "123";"myohmy" = 17; return 0;
}
Compile error,
1>d:\visual studio 2008\projects\test_string2\test_string2\main.cpp(5) : error C2106: '=' : left operand must be l-value
1>d:\visual studio 2008\projects\test_string2\test_string2\main.cpp(7) : error C2440: '=' : cannot convert from 'int' to 'const char [7]'regards, George
George_George wrote:
Are you sure it can compile?
Of course not. I never said it could. :rolleyes:
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
George_George wrote:
What do you think of whether string literal is lvalue or rvalue? I think it is only rvalue, do you agree?
Yes, of course. Did you ever see a string literal on the left of an assignment expression? :-D
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain ClarkeThanks CPallini, I agree with your reply. I am studying the link as people mention before, http://www.velocityreviews.com/forums/t279868-what-is-lvalue.html[^] in post #2, what do the following statements mean? Especially the additional [2] after "str" and ["str"] after 1? Any ideas?
* ( ( char * ) & var ) = "str"[2]; // * ((char *)&var) too
int & varref = var;
varref = 1["str"]; // varref is also an lvalue
regards, George
-
George_George wrote:
Are you sure it can compile?
Of course not. I never said it could. :rolleyes:
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
Thanks DavidCrow, So, 1. You agree string literal is rvalue? 2. Any comments for, http://www.codeproject.com/script/Forums/View.aspx?fid=1647&msg=2448186[^] regards, George
-
Thanks DavidCrow, So, 1. You agree string literal is rvalue? 2. Any comments for, http://www.codeproject.com/script/Forums/View.aspx?fid=1647&msg=2448186[^] regards, George
George_George wrote:
1. You agree string literal is rvalue?
I already did.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
George_George wrote:
1. You agree string literal is rvalue?
I already did.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
Thanks DavidCrow, Any comments to? http://www.codeproject.com/script/Forums/View.aspx?fid=1647&msg=2448186[^] regards, George
-
Thanks CPallini, I agree with your reply. I am studying the link as people mention before, http://www.velocityreviews.com/forums/t279868-what-is-lvalue.html[^] in post #2, what do the following statements mean? Especially the additional [2] after "str" and ["str"] after 1? Any ideas?
* ( ( char * ) & var ) = "str"[2]; // * ((char *)&var) too
int & varref = var;
varref = 1["str"]; // varref is also an lvalue
regards, George
George_George wrote:
in post #2, what do the following statements mean? Especially the additional [2] after "str" and ["str"] after 1? Any ideas?
George_George wrote:
* ( ( char * ) & var ) = "str"[2]; // * ((char *)&var) too
it is nothing but subscripting an array, here "str" is an constant array of string. it is same as
const char * const szText = "str"; or const char szText[] = {"str"};
* ( ( char * ) & var ) = szText[2];George_George wrote:
varref = 1["str"]; // varref is also an lvalue
"the subscript operator [] is interpreted in such a way that E1[E2] is identical to *((E1)+(E2))" from C++ std. I think 1["str"] is tricking the above expression to *((1) + (pointer to "str")) that is same as "str"[1] which evaluates to *((pointer to "str") + (1)). because adding pointer to index and index to pointer is same.
-
George_George wrote:
in post #2, what do the following statements mean? Especially the additional [2] after "str" and ["str"] after 1? Any ideas?
George_George wrote:
* ( ( char * ) & var ) = "str"[2]; // * ((char *)&var) too
it is nothing but subscripting an array, here "str" is an constant array of string. it is same as
const char * const szText = "str"; or const char szText[] = {"str"};
* ( ( char * ) & var ) = szText[2];George_George wrote:
varref = 1["str"]; // varref is also an lvalue
"the subscript operator [] is interpreted in such a way that E1[E2] is identical to *((E1)+(E2))" from C++ std. I think 1["str"] is tricking the above expression to *((1) + (pointer to "str")) that is same as "str"[1] which evaluates to *((pointer to "str") + (1)). because adding pointer to index and index to pointer is same.
Great Rajkumar!! Cool reply. For this discussion, http://www.velocityreviews.com/forums/t279868-what-is-lvalue.html[^] It is mentioned,
struct C
{
C& operator=(int);
};C() = 5; // OK to assign to r-value C();
Here is the related description, -------------------- True. But there are compilers (eg VC++) that do allow to bind non-const references to temporary as an extension of the standard. IMO, a conforming compiler should reject the code. -------------------- I can not see in above code there occurs "bind non-const references to temporary". Do you see it occurs? regards, George