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-0x-features-in-vc10-part-2.aspx
The explanation is in the context of rvalue references but I found it
very easy to grasp.
<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.
>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?
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.
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