Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #11616 > unrolled thread

Mutability of temporary variables

Started bykyle <kyle@nomail.com>
First post2011-11-19 13:33 +0100
Last post2011-11-22 23:26 +0100
Articles 5 on this page of 25 — 7 participants

Back to article view | Back to comp.lang.c++


Contents

  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

Page 2 of 2 — ← Prev page 1 [2]


#11629

From"Paul" <pchrist<nospam>or@yahoo.co.uk>
Date2011-11-20 00:31 +0000
Message-ID<ja9hpn$tq3$1@adenine.netfront.net>
In reply to#11620
"Johannes Schaub" <schaub.johannes@googlemail.com> wrote in message 
news:ja8dkh$snr$1@news.albasani.net...
> kyle wrote:
>
>> 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.
>>
>
> The error in your thinking is that you think that "int()" is a temporary
> object.
>
> However, "int()" is not a temporary object. It is simply a value, and when
> it is an initializer of a reference, a temporary object will be 
> initialized
> with that value, and the object will have the type and constness of the
> referred type of the reference.

"int()" is not a value. Its a piece of source code that represents 
construction of an integer data type.
Whether the data is temporary or non temporary depends how far into the 
compilation process you go.

What do you mean by temporary?temprorarily created in the compilation 
process, or temporarily created in the programs executable process? 
temporarily created in the cpu's translation process ?
wtf is temporary supposed to mean?




--- Posted via news://freenews.netfront.net/ - Complaints to news@netfront.net ---

[toc] | [prev] | [next] | [standalone]


#11764

Fromkyle <kyle@nomail.com>
Date2011-11-24 00:50 +0100
Message-ID<op.v5fa6tmvpbcp0d@localhost.localdomain>
In reply to#11620
Dnia 19-11-2011 o 16:16:45 Johannes Schaub  
<schaub.johannes@googlemail.com> napisał(a):

> kyle wrote:
>
>> Consider following code:
>>
>> int main() {
>>    const int& c = int();
>>    int& m = const_cast<int&>(c);
>>    m = 4;
>> }
>>
>
> The error in your thinking is that you think that "int()" is a temporary
> object.
>

This is indeed what i assumed.
As i understand, i could 'fix' the code by having a function 'int foo()  
{return 1;}' and using it in place of 'int()'?

Unfortunately, what i originally planned to do is illegal for entirely  
different reason ;)

-- 
kyle

[toc] | [prev] | [next] | [standalone]


#11796

FromJohannes Schaub <schaub.johannes@googlemail.com>
Date2011-11-24 20:36 +0100
Message-ID<jam2ml$rm2$1@news.albasani.net>
In reply to#11764
kyle wrote:

> Dnia 19-11-2011 o 16:16:45 Johannes Schaub
> <schaub.johannes@googlemail.com> napisał(a):
> 
>> kyle wrote:
>>
>>> Consider following code:
>>>
>>> int main() {
>>>    const int& c = int();
>>>    int& m = const_cast<int&>(c);
>>>    m = 4;
>>> }
>>>
>>
>> The error in your thinking is that you think that "int()" is a temporary
>> object.
>>
> 
> This is indeed what i assumed.
> As i understand, i could 'fix' the code by having a function 'int foo()
> {return 1;}' and using it in place of 'int()'?
> 

No, as someone else already said, rvalues of non-class and non-array type 
are not objects, and the rules for initialization of references adequately 
are worded to take that into account. It will behave exactly like the 
"int()" case.

[toc] | [prev] | [next] | [standalone]


#11656

FromSG <s.gesemann@gmail.com>
Date2011-11-21 01:17 -0800
Message-ID<a3a8700b-4dba-4d25-a134-e267d24f0be9@o1g2000vbe.googlegroups.com>
In reply to#11616
On Nov 19, 1:33 pm, kyle wrote:
>
>  int main() {
>    const int& c = int();
>    int& m = const_cast<int&>(c);
>    m = 4;
>  }
>
> [...]
>
> Temporaries are mutable, so in whole my snippet is legal C++. Am i correct?

Yes, I believe so. I would not go as far as saying "temporaries are
mutable" because one can easily create const class-type temporaries.
These are obviously not mutable unless they have mutable data members.
But, I agree with Paavo on this one. As far as I can tell, no
undefined behaviour is invoked here.

Cheers!
SG

[toc] | [prev] | [next] | [standalone]


#11723

FromJohannes Schaub <schaub.johannes@googlemail.com>
Date2011-11-22 23:26 +0100
Message-ID<jah3uq$ks1$1@news.albasani.net>
In reply to#11656
SG wrote:

> On Nov 19, 1:33 pm, kyle wrote:
>>
>>  int main() {
>> const int& c = int();
>> int& m = const_cast<int&>(c);
>> m = 4;
>>  }
>>
>> [...]
>>
>> Temporaries are mutable, so in whole my snippet is legal C++. Am i
>> correct?
> 
> Yes, I believe so. I would not go as far as saying "temporaries are
> mutable" because one can easily create const class-type temporaries.
> These are obviously not mutable unless they have mutable data members.
> But, I agree with Paavo on this one. As far as I can tell, no
> undefined behaviour is invoked here.
> 

What do you say about the point I have made about the code? 

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | comp.lang.c++


csiph-web