Is a string literal an lvalue?

S

Stanley Rice

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.
 
S

SG

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
 
D

Dilip

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/arch...-references-c-0x-features-in-vc10-part-2.aspx

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

Stanley Rice

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:

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.
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)
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?
 
S

SG

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.

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.
[...]
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)

I cannot under the statement above. why the 'x' here is xvalue?

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top