Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #11627
| From | "Paul" <pchrist<nospam>or@yahoo.co.uk> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: Mutability of temporary variables |
| Date | 2011-11-20 00:14 +0000 |
| Organization | Netfront http://www.netfront.net/ |
| Message-ID | <ja9gps$shf$1@adenine.netfront.net> (permalink) |
| References | <op.v4605gilpbcp0d@localhost.localdomain> <Xns9FA29BB4A282Bmyfirstnameosapriee@216.196.109.131> <ja8bu0$88k$2@dont-email.me> <Do2dnVH-ff_DIVrTnZ2dnUVZ8qOdnZ2d@giganews.com> |
"Leigh Johnston" <leigh@i42.co.uk> wrote in message
news:Do2dnVH-ff_DIVrTnZ2dnUVZ8qOdnZ2d@giganews.com...
> On 19/11/2011 13:45, Victor Bazarov wrote:
>> On 11/19/2011 8:18 AM, Paavo Helde wrote:
>>> kyle<kyle@nomail.com> wrote in
>>> news:op.v4605gilpbcp0d@localhost.localdomain:
>>>
>>>> Consider following code:
>>>>
>>>> int main() {
>>>> const int& c = int();
>>>> int& m = const_cast<int&>(c);
>>>> m = 4;
>>>> }
>>>>
>>>> The object of the snippet is to get a mutable reference to a
>>>> temporary. This cant be done directly because non-const reference
>>>> cannot bind to temporary, but we should be OK with casting away
>>>> constness of reference 'c' since it doesn't actually refer to const
>>>> object.
>>>>
>>>> Temporaries are mutable, so in whole my snippet is legal C++. Am i
>>>> correct?
>>>
>>> Yes, I think you are correct. Another trick to get a mutable reference
>>> to
>>> a temporary is to use a non-const member function. This avoids
>>> const_cast, but of course lifetime extending by binding to a const
>>> reference does not work any more:
>>>
>>> struct A {
>>> int m;
>>> A& Ref() {return *this;}
>>> };
>>>
>>> void f(A& a) {
>>> a.m = 4;
>>> }
>>>
>>> int main() {
>>> f( A().Ref() );
>>> }
>>>
>>
>> There is a difference between your example and the OP's. In your example
>> the temporary is of a class type, and the expression A() produces an
>> lvalue to begin with.
>
> No; A() produces an rvalue.
>
Um no , the value produced is neither an l-value or an r-value , its
inbetween and its known as an intermediate-value.
Its value is neither left nor right because its not actually a value at all.
Its an abstractatin of the representented data, which was never actually
data in the first place because it was just an identifier which "pointed to"
the data or "represnted" said data or datas.
But the identifier was not even an identifier because it was a "name" which
represents an identifier which , in turn , represents data.
Get a grip noob.
--- Posted via news://freenews.netfront.net/ - Complaints to news@netfront.net ---
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Mutability of temporary variables kyle <kyle@nomail.com> - 2011-11-19 13:33 +0100
Re: Mutability of temporary variables Paavo Helde <myfirstname@osa.pri.ee> - 2011-11-19 07:18 -0600
Re: Mutability of temporary variables Victor Bazarov <v.bazarov@comcast.invalid> - 2011-11-19 08:45 -0500
Re: Mutability of temporary variables Leigh Johnston <leigh@i42.co.uk> - 2011-11-19 14:25 +0000
Re: Mutability of temporary variables "Paul" <pchrist<nospam>or@yahoo.co.uk> - 2011-11-20 00:14 +0000
Re: Mutability of temporary variables Leigh Johnston <leigh@i42.co.uk> - 2011-11-20 00:45 +0000
Re: Mutability of temporary variables Leigh Johnston <leigh@i42.co.uk> - 2011-11-20 01:14 +0000
Re: Mutability of temporary variables "Paul" <pchrist<nospam>or@yahoo.co.uk> - 2011-11-20 02:34 +0000
Re: Mutability of temporary variables "Paul" <pchrist<nospam>or@yahoo.co.uk> - 2011-11-20 02:44 +0000
Re: Mutability of temporary variables Leigh Johnston <leigh@i42.co.uk> - 2011-11-20 13:06 +0000
Re: Mutability of temporary variables Leigh Johnston <leigh@i42.co.uk> - 2011-11-20 13:21 +0000
Re: Mutability of temporary variables "Paul" <pchrist<nospam>or@yahoo.co.uk> - 2011-11-20 16:51 +0000
Re: Mutability of temporary variables Leigh Johnston <leigh@i42.co.uk> - 2011-11-20 17:24 +0000
Re: Mutability of temporary variables SG <s.gesemann@gmail.com> - 2011-11-20 00:49 -0800
Re: Mutability of temporary variables Victor Bazarov <v.bazarov@comcast.invalid> - 2011-11-19 08:42 -0500
Re: Mutability of temporary variables Paavo Helde <myfirstname@osa.pri.ee> - 2011-11-19 08:20 -0600
Re: Mutability of temporary variables Victor Bazarov <v.bazarov@comcast.invalid> - 2011-11-19 15:51 -0500
Re: Mutability of temporary variables Paavo Helde <myfirstname@osa.pri.ee> - 2011-11-19 16:51 -0600
Re: Mutability of temporary variables "Paul" <pchrist<nospam>or@yahoo.co.uk> - 2011-11-20 00:23 +0000
Re: Mutability of temporary variables Johannes Schaub <schaub.johannes@googlemail.com> - 2011-11-19 16:16 +0100
Re: Mutability of temporary variables "Paul" <pchrist<nospam>or@yahoo.co.uk> - 2011-11-20 00:31 +0000
Re: Mutability of temporary variables kyle <kyle@nomail.com> - 2011-11-24 00:50 +0100
Re: Mutability of temporary variables Johannes Schaub <schaub.johannes@googlemail.com> - 2011-11-24 20:36 +0100
Re: Mutability of temporary variables SG <s.gesemann@gmail.com> - 2011-11-21 01:17 -0800
Re: Mutability of temporary variables Johannes Schaub <schaub.johannes@googlemail.com> - 2011-11-22 23:26 +0100
csiph-web