Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. lvalue rvalue discussion

lvalue rvalue discussion

Scheduled Pinned Locked Moved C / C++ / MFC
questiondiscussion
21 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N nbugalia

    George_George wrote:

    You know, lvalue have modifiable and un-modifiable versions.

    I know that. If you will try to assign something to a constant say - int i = 3; 5 = i; It will definately give you lvalue cannot be modified error.

    G Offline
    G Offline
    George_George
    wrote on last edited by
    #7

    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

    D 1 Reply Last reply
    0
    • G George_George

      Thanks manish, I do not quite understand this statement in the thread "in C++, string literals are l-values.", any comments? regards, George

      M Offline
      M Offline
      manish patel
      wrote on last edited by
      #8

      did you read this stetement : "l-values are something that can be at the right side of an '='" It 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. Now i think you got it

      Manish Patel. B.E. - Information Technology.

      G CPalliniC 2 Replies Last reply
      0
      • M manish patel

        did you read this stetement : "l-values are something that can be at the right side of an '='" It 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. Now i think you got it

        Manish Patel. B.E. - Information Technology.

        G Offline
        G Offline
        George_George
        wrote on last edited by
        #9

        Thanks Manish, I am confused. You mentioned, -------------------- l-values are something that can be at the right side of an '=' -------------------- l-value...right side? Could you confirm that? regards, George

        1 Reply Last reply
        0
        • M manish patel

          did you read this stetement : "l-values are something that can be at the right side of an '='" It 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. Now i think you got it

          Manish Patel. B.E. - Information Technology.

          CPalliniC Online
          CPalliniC Online
          CPallini
          wrote on last edited by
          #10

          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 term x 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 Clarke

          In testa che avete, signor di Ceprano?

          G 1 Reply Last reply
          0
          • G George_George

            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

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #11

            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

            G 1 Reply Last reply
            0
            • D David Crow

              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

              G Offline
              G Offline
              George_George
              wrote on last edited by
              #12

              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

              D 1 Reply Last reply
              0
              • CPalliniC CPallini

                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 term x 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 Clarke

                G Offline
                G Offline
                George_George
                wrote on last edited by
                #13

                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

                CPalliniC 1 Reply Last reply
                0
                • G George_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

                  CPalliniC Online
                  CPalliniC Online
                  CPallini
                  wrote on last edited by
                  #14

                  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

                  In testa che avete, signor di Ceprano?

                  G 1 Reply Last reply
                  0
                  • G George_George

                    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

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #15

                    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

                    G 1 Reply Last reply
                    0
                    • CPalliniC CPallini

                      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

                      G Offline
                      G Offline
                      George_George
                      wrote on last edited by
                      #16

                      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

                      R 1 Reply Last reply
                      0
                      • D David Crow

                        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

                        G Offline
                        G Offline
                        George_George
                        wrote on last edited by
                        #17

                        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

                        D 1 Reply Last reply
                        0
                        • G George_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

                          D Offline
                          D Offline
                          David Crow
                          wrote on last edited by
                          #18

                          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

                          G 1 Reply Last reply
                          0
                          • D David Crow

                            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

                            G Offline
                            G Offline
                            George_George
                            wrote on last edited by
                            #19

                            Thanks DavidCrow, Any comments to? http://www.codeproject.com/script/Forums/View.aspx?fid=1647&msg=2448186[^] regards, George

                            1 Reply Last reply
                            0
                            • G George_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

                              R Offline
                              R Offline
                              Rajkumar R
                              wrote on last edited by
                              #20

                              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.

                              G 1 Reply Last reply
                              0
                              • R Rajkumar R

                                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.

                                G Offline
                                G Offline
                                George_George
                                wrote on last edited by
                                #21

                                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

                                1 Reply Last reply
                                0
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • World
                                • Users
                                • Groups