Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Is a string literal an lvalue?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Stanley Rice  
View profile  
 More options Oct 8 2011, 3:35 am
Newsgroups: comp.lang.c++
From: Stanley Rice <hecong...@gmail.com>
Date: Sat, 8 Oct 2011 00:35:46 -0700 (PDT)
Local: Sat, Oct 8 2011 3:35 am
Subject: Is a string literal an lvalue?
5.1.1/1 says that:
A literal is a primary expression. Its type depends on its form
(2.14). A string literal is an lvalue; all other literals are pvalues.

Does a string literal really a lvalue? If I code:
    "foo" = "bar";
the compiler shuts at it.

Or do i understand the string literal incorrectly.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
SG  
View profile  
 More options Oct 8 2011, 7:13 am
Newsgroups: comp.lang.c++
From: SG <s.gesem...@gmail.com>
Date: Sat, 8 Oct 2011 04:13:30 -0700 (PDT)
Local: Sat, Oct 8 2011 7:13 am
Subject: Re: Is a string literal an lvalue?
On Oct 8, 9:35 am, Stanley Rice wrote:

> 5.1.1/1 says that:
> A literal is a primary expression. Its type depends on its form
> (2.14). A string literal is an lvalue; all other literals are pvalues.

> Does a string literal really a lvalue?

Yes.

> If I code:
>     "foo" = "bar";
> the compiler shuts at it.

"lvalue" does not imply assignable. Also, you're dealing with arrays.
You don't expect

  int i[] = {1,2,3};
  int j[] = {3,2,1};
  i = j; // <-- this

to work, do you?

> Or do i understand the string literal incorrectly.

I don't know. But you probably don't fully understand the lvalue
concept. Unfortunately, there is no short definnition for "lvalue" --
only a rule of thumb: "Can I take its address? If yes, it's an lvalue
(or an xvalue)".

Let me try to explain value categories in my own words:

The "value category" is a property of an *expression*. There are (now
in C++2011) three value categories:
- Lvalue
- Xvalue (eXpendable or eXpiring)
- PRvalue (pure rvalue)
In addition we use
- GLvalue (to refer to Lvalues and Xvalues)
- Rvalue (to refer to Xvalues and PRvalues)

GLvalues versus PRvalues:
-------------------------
GLvalues refer to objects that have an identity (region of storage)
PRvalues refer to plain values or temporary objects

This "axis" affects things like polymorphism, ability to take the
address of an object. GLvalues can have an abstract or incomplete
static type. PRvalues cannot. The dynamic type of a PRvalues is always
the same as its static type. For GLvalues the static and dynamic type
may be different.

Lvalues versus Rvalues:
-----------------------
Lvalues refer to things that probably continue to be of interest. They
always refer to an "object", to a region of storage. Modifying an
object referred to by an Lvalue expression is easily observable by the
rest of the program.

Rvalues refer to things that the rest of the program will not care
about anymore. Modifying an object referred to by an Rvalue is okay
because this object either has no identity or the user explicitly used
a cast to say "I don't care about this thing anymore". The ability to
detect Rvalues and temporary objects is what enables "move
semantics" (a C++2011 feature).

Examples:

  string source();

  int main() {
    source();    // PRvalue

    string x = "yay";

    x;           // Lvalue

    move(x)      // Xvalue (still refers to the same object but
                 // the expression has a different value category)

    x+"123"      // PRvalue (operator+ returns string by value)

    x[1];        // Lvalue (operator[] returns a char&)
  }

Cheers!
SG


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dilip  
View profile  
 More options Oct 8 2011, 8:10 am
Newsgroups: comp.lang.c++
From: Dilip <rdil...@lycos.com>
Date: Sat, 8 Oct 2011 05:10:40 -0700 (PDT)
Local: Sat, Oct 8 2011 8:10 am
Subject: Re: Is a string literal an lvalue?
On Oct 8, 3:35 am, Stanley Rice <hecong...@gmail.com> wrote:

> 5.1.1/1 says that:
> A literal is a primary expression. Its type depends on its form
> (2.14). A string literal is an lvalue; all other literals are pvalues.

> Does a string literal really a lvalue? If I code:
>     "foo" = "bar";
> the compiler shuts at it.

> Or do i understand the string literal incorrectly.

In addition to SG's reply, you can take a look at this post:
http://blogs.msdn.com/b/vcblog/archive/2009/02/03/rvalue-references-c...

The explanation is in the context of rvalue references but I found it
very easy to grasp.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Stanley Rice  
View profile  
 More options Oct 8 2011, 10:22 am
Newsgroups: comp.lang.c++
From: Stanley Rice <hecong...@gmail.com>
Date: Sat, 8 Oct 2011 07:22:50 -0700 (PDT)
Local: Sat, Oct 8 2011 10:22 am
Subject: Re: Is a string literal an lvalue?
On Oct 8, 7:13 pm, SG <s.gesem...@gmail.com> wrote:

<snippt>

> PRvalues refer to plain values or temporary objects

3.10.----------------------
An rvalue is an xvalue, a temporary object or subobject thereof, or a
value that is not associated with an object.
A prvalue is an rvalue that is not an xvalue.
------------------------------
so, should prvalue includes subobject? But I don't understand what a
subobject here means.

> This "axis" affects things like polymorphism, ability to take the
> address of an object. GLvalues can have an abstract or incomplete
> static type.

Here what do you mean by 'abstract type'? I referred to the SO, but
found nothing.

I cannot under the statement above. why the 'x' here is xvalue? Is is
consistent to the standard quotes below:

3.10.1
An xvalue also refers to an object, usually near the end of its
lifetime (so that its resources may be moved, for example). An xvalue
is the result of certain kinds of expression invoking rvalue
references.

Actually, I could not understand the whole paragraph of the standard,
for I just began to read standard for a several days.

>     x+"123"      // PRvalue (operator+ returns string by value)

>     x[1];        // Lvalue (operator[] returns a char&)
>   }

> Cheers!
> SG

All in all, greatly thanks for your detail explanation. The standard
is new to me, and there are lots of new terms that I haven't heard
before. Any suggestions to how to read it?

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Stanley Rice  
View profile  
 More options Oct 8 2011, 10:26 am
Newsgroups: comp.lang.c++
From: Stanley Rice <hecong...@gmail.com>
Date: Sat, 8 Oct 2011 07:26:28 -0700 (PDT)
Local: Sat, Oct 8 2011 10:26 am
Subject: Re: Is a string literal an lvalue?
On Oct 8, 8:10 pm, Dilip <rdil...@lycos.com> wrote:

As I am not a native English speaker, the blog is new to me and I
found there are bunches of valuable blogs. Thanks for your
recommendation.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
SG  
View profile  
 More options Oct 8 2011, 11:30 am
Newsgroups: comp.lang.c++
From: SG <s.gesem...@gmail.com>
Date: Sat, 8 Oct 2011 08:30:29 -0700 (PDT)
Local: Sat, Oct 8 2011 11:30 am
Subject: Re: Is a string literal an lvalue?
On Oct 8, 4:22 pm, Stanley Rice wrote:

> On Oct 8, 7:13 pm, SG wrote:

> > GLvalues versus PRvalues:
> > -------------------------
> > GLvalues refer to objects that have an identity (region of storage)
> > PRvalues refer to plain values or temporary objects

> 3.10.----------------------
> An rvalue is an xvalue, a temporary object or subobject thereof, or a
> value that is not associated with an object.
> A prvalue is an rvalue that is not an xvalue.
> ------------------------------
> so, should prvalue includes subobject? But I don't understand what a
> subobject here means.

Non-static data members and the base-class portions of class-type
objects as well as elements of an array are considered subobjects.

> > [GLvalue <-> PRvalue]
> > This "axis" affects things like polymorphism, ability to take the
> > address of an object. GLvalues can have an abstract or incomplete
> > static type. [...]

> Here what do you mean by 'abstract type'? I referred to the SO, but
> found nothing.

In the context of C++, I use "abstract type" to mean a polymorphic
type that cannot be directly instantiated because it has at least one
*pure* virtual function. A polymorphic type is a type that has at
least one virtual member function or destructor.

It is not. The whole expression -- std::move(x) -- is an Xvalue. The
subexpression x will always be an Lvalue in that scope.

> [...]
> Actually, I could not understand the whole paragraph of the standard,
> for I just began to read standard for a several days.
> [...]
> All in all, greatly thanks for your detail explanation. The standard
> is new to me, and there are lots of new terms that I haven't heard
> before. Any suggestions to how to read it?

Sorry, no. It requires some iterations, back & forth and time to get
the hang of it. If you're interested in learning basic C++ there are
much better books than that. If you're interested in learning about
the new features, try to look for articles explaining these features
written by knowledgable people. Books have yet to be written. The
lvalue/rvalue/move semantics topic is covered in a couple of articles
as well as talks that have been recorded and are publicly available.
Authors and speakers include Dave Abrahams, Howard Hinnant, Scott
Meyers, Stephan T. Lavavej.

Cheers!
SG


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »