Topic: rvalues and temporary objects


Author: FredOverflow <fredrik.winkler@googlemail.com>
Date: Thu, 10 Jun 2010 14:10:57 CST
Raw View
As I understand it, C++03 defines an rvalue as an expression that,
when evaluated, denotes a temporary object that is destroyed at the
end of the full expression in which the rvalue is nested. Is this
understanding correct?

The wording of the C++0x final committee draft confuses me:
"An rvalue [...] is an xvalue, a temporary object or subobject
thereof, or a value that is not associated with an object."

Basically the FCD equates expressions and objects, which I find
confusing. Is this a known issue?

By the way, I asked this question on stack overflow 1 hour ago and was
redirected here :-)
http://stackoverflow.com/questions/3007728/rvalues-and-temporary-objects-in-the-fcd

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: =3D?ISO-8859-1?Q?Daniel_Kr=3DFCgler?=3D <daniel.kruegler@googlemail.c=.om>
Date: Fri, 11 Jun 2010 16:33:20 CST
Raw View
On 10 Jun., 22:10, FredOverflow <fredrik.wink...@googlemail.com>
wrote:
> As I understand it, C++03 defines an rvalue as an expression that,
> when evaluated, denotes a temporary object that is destroyed at the
> end of the full expression in which the rvalue is nested. Is this
> understanding correct?

Not really, because even in C++03

a) literals are no objects and don't refer to objects

b) the situation of the value category of the exception object
described
in 15.1 [except.throw]/3 and the temporaries occurring in copy-
initialization
(8.5 [dcl.init]/14 bullet 4 subbulet 3) is at least unclear (It turns
out that

http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#177

fixed that)

> The wording of the C++0x final committee draft confuses me:
> "An rvalue [...] is an xvalue, a temporary object or subobject
> thereof, or a value that is not associated with an object."
>
> Basically the FCD equates expressions and objects, which I find
> confusing.

I agree that the description is somewhat misleading. At least
the exception object is a temporary that is an lvalue and I
also consider usually the domains of objects and expressions
as separate ones. In several other situations the standard
more correctly speaks of an expression that *denotes* an
object or that *refers to* an object.

> Is this a known issue?

Not that I know of.

> By the way, I asked this question on stack overflow 1 hour ago and was
> redirected here :-)
http://stackoverflow.com/questions/3007728/rvalues-and-temporary-obje...

I agree that this question is well-located in this discussion group.

HTH & Greetings from Bremen,

Daniel Kr=FCgler


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "Johannes Schaub (litb)" <schaub-johannes@web.de>
Date: Fri, 11 Jun 2010 18:00:00 CST
Raw View
FredOverflow wrote:

> As I understand it, C++03 defines an rvalue as an expression that,
> when evaluated, denotes a temporary object that is destroyed at the
> end of the full expression in which the rvalue is nested. Is this
> understanding correct?
>
> The wording of the C++0x final committee draft confuses me:
> "An rvalue [...] is an xvalue, a temporary object or subobject
> thereof, or a value that is not associated with an object."
>
> Basically the FCD equates expressions and objects, which I find
> confusing. Is this a known issue?
>

In my opinion this is a wording bug. The same kind of wording bug is done at
15.1/3 "The temporary is an lvalue ...". Both wordings need to be fixed in
my opinion.

Also, not all temporary objects are referred-to by only rvalues, so this
definition remarkably is not bidirectional. Some aren't even referred to by
any expression (like exception objects), and some are referred to by lvalues
(like "const T& t = T(); t;").


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Nikolay Ivchenkov <tsoae@mail.ru>
Date: Sun, 13 Jun 2010 15:52:11 CST
Raw View
Daniel Krugler wrote:
>
> Not really, because even in C++03
>
> a) literals are no objects and don't refer to objects

String literals are lvalues that denote objects.

I would like to ask whether the expression int() denotes a temporary
object in the example below

   int main()
   {
       enum { e = int() };
   }

Note: value-initialization is defined for objects only (see 8.5).

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: =3D?ISO-8859-1?Q?Daniel_Kr=3DFCgler?=3D <daniel.kruegler@googlemail.c=.om>
Date: Mon, 14 Jun 2010 14:38:32 CST
Raw View
On 13 Jun., 23:52, Nikolay Ivchenkov <ts...@mail.ru> wrote:
> Daniel Krugler wrote:
>
> > Not really, because even in C++03
>
> > a) literals are no objects and don't refer to objects
>
> String literals are lvalues that denote objects.

Nod, thanks for this correction.

> I would like to ask whether the expression int() denotes a temporary
> object in the example below
>
>    int main()
>    {
>        enum { e = int() };
>    }
>
> Note: value-initialization is defined for objects only (see 8.5).

See open core issue

http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#943

The intention is: Yes it is a temporary.

HTH & Greetings from Bremen,

Daniel Kr=FCgler


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "Johannes Schaub (litb)" <schaub-johannes@web.de>
Date: Mon, 14 Jun 2010 14:40:33 CST
Raw View
Nikolay Ivchenkov wrote:

> Daniel Krugler wrote:
>>
>> Not really, because even in C++03
>>
>> a) literals are no objects and don't refer to objects
>
> String literals are lvalues that denote objects.
>
> I would like to ask whether the expression int() denotes a temporary
> object in the example below
>
>    int main()
>    {
>        enum { e = int() };
>    }
>
> Note: value-initialization is defined for objects only (see 8.5).
>

There is an issue report about this: http://www.open-
std.org/jtc1/sc22/wg21/docs/cwg_active.html#943 . Apparently it's not
supposed to denote a temporary object.

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Nikolay Ivchenkov <tsoae@mail.ru>
Date: Mon, 14 Jun 2010 18:48:56 CST
Raw View
On 15 Jun, 00:38, Daniel Krugler wrote:
> On 13 Jun., 23:52, Nikolay Ivchenkov <ts...@mail.ru> wrote:
>
> > I would like to ask whether the expression int() denotes a temporary
> > object in the example below
>
> >    int main()
> >    {
> >        enum { e = int() };
> >    }
>
> > Note: value-initialization is defined for objects only (see 8.5).
>
> See open core issue
>
> http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#943
>
> The intention is: Yes it is a temporary.

If so, when its lifetime begins and ends? Similar question can be
asked about

   int main()
   {
       enum { e = static_cast<int>(1.) };
       // is supposed to be well-formed
   }

It is also unclear how the effect of such an explicit conversion is
defined. Note: the effect is not the same as

   int main()
   {
       int temporary(1);
       enum { e = temporary };
       // ill-formed
   }


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: =?ISO-8859-1?Q?Daniel_Kr=FCgler?= <daniel.kruegler@googlemail.com>
Date: Wed, 16 Jun 2010 02:25:04 CST
Raw View
On Jun 15, 2:48 am, Nikolay Ivchenkov <ts...@mail.ru> wrote:
> On 15 Jun, 00:38, Daniel Krugler wrote:
>
> > On 13 Jun., 23:52, Nikolay Ivchenkov <ts...@mail.ru> wrote:
>
> > > I would like to ask whether the expression int() denotes a temporary
> > > object in the example below
>
> > >    int main()
> > >    {
> > >        enum { e = int() };
> > >    }
>
> > > Note: value-initialization is defined for objects only (see 8.5).
>
> > See open core issue
>
> >http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#943
>
> > The intention is: Yes it is a temporary.
>
> If so, when its lifetime begins and ends?

Sorry, I should have read the mentioned issue more carefully.
Doing so makes it clear, that the intention is that *none* of T(),
T{},
T(0), etc should produce a temporary for T, if they are of *non-
class*
type. So, int() is supposed to be an rvalue without object due to
a constant expression of  type int (but see also core  issue 236).

Lifetime issue could still occur (but not observable ones IMO), if
we bring literal class types into the scenario:

struct IntLike {
 int v;
 constexpr IntLike(int v = 0) : v(v) {}
 constexpr operator int() { return v; }
};

and produce a revised form of your example:

int main()
{
 enum { e = IntLike() };
}

The lifetime of the temporary of type IntLike should
be defined by the /full-expression/ that appears within the
initializer of the enumerator value e, which is the constant
expression that is necessary to compute the value of e.

> Similar question can be asked about
>
>    int main()
>    {
>        enum { e = static_cast<int>(1.) };
>        // is supposed to be well-formed
>    }
>
> It is also unclear how the effect of such an explicit conversion is
> defined.

I think this is the same situation as above, because
static_cast<int>(1.) is a constant expression of a non-
class type. Again, with literal class types this changes
somewhat. I don't think that this change is observable,
because those types must have trivial/constexpr
special member functions.

HTH & Greetings from Bremen,

Daniel Kr   gler



--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Nikolay Ivchenkov <tsoae@mail.ru>
Date: Thu, 17 Jun 2010 09:51:06 CST
Raw View
On 16 Jun, 12:25, Daniel Kr=FCgler <daniel.krueg...@googlemail.com>
wrote:
>
> So, int() is supposed to be an rvalue without object due to
> a constant expression of  type int

According to N3092 - 5.2.3/2,

"The expression T(), where T is a simple-type-specifier or typename-
specifier for a non-array complete object type or the (possibly cv-
qualified) void type, creates a prvalue of the specified type, which
is value-initialized (8.5; no initialization is done for the void()
case)."

What does the value-initialization mean when the prvalue of the
specified object type does not refer to an object?

See 8.5/7:

"To value-initialize an *object* of type T means:
[...]"


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]