Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #11616 > unrolled thread
| Started by | kyle <kyle@nomail.com> |
|---|---|
| First post | 2011-11-19 13:33 +0100 |
| Last post | 2011-11-22 23:26 +0100 |
| Articles | 20 on this page of 25 — 7 participants |
Back to article view | Back to comp.lang.c++
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 1 of 2 [1] 2 Next page →
| From | kyle <kyle@nomail.com> |
|---|---|
| Date | 2011-11-19 13:33 +0100 |
| Subject | Mutability of temporary variables |
| Message-ID | <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?
--
kyle
[toc] | [next] | [standalone]
| From | Paavo Helde <myfirstname@osa.pri.ee> |
|---|---|
| Date | 2011-11-19 07:18 -0600 |
| Message-ID | <Xns9FA29BB4A282Bmyfirstnameosapriee@216.196.109.131> |
| In reply to | #11616 |
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() );
}
[toc] | [prev] | [next] | [standalone]
| From | Victor Bazarov <v.bazarov@comcast.invalid> |
|---|---|
| Date | 2011-11-19 08:45 -0500 |
| Message-ID | <ja8bu0$88k$2@dont-email.me> |
| In reply to | #11617 |
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.
V
--
I do not respond to top-posted replies, please don't ask
[toc] | [prev] | [next] | [standalone]
| From | Leigh Johnston <leigh@i42.co.uk> |
|---|---|
| Date | 2011-11-19 14:25 +0000 |
| Message-ID | <Do2dnVH-ff_DIVrTnZ2dnUVZ8qOdnZ2d@giganews.com> |
| In reply to | #11619 |
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.
/Leigh
[toc] | [prev] | [next] | [standalone]
| From | "Paul" <pchrist<nospam>or@yahoo.co.uk> |
|---|---|
| Date | 2011-11-20 00:14 +0000 |
| Message-ID | <ja9gps$shf$1@adenine.netfront.net> |
| In reply to | #11622 |
"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 ---
[toc] | [prev] | [next] | [standalone]
| From | Leigh Johnston <leigh@i42.co.uk> |
|---|---|
| Date | 2011-11-20 00:45 +0000 |
| Message-ID | <9vidndGK4sk50FXTnZ2dnUVZ8nadnZ2d@giganews.com> |
| In reply to | #11627 |
On 20/11/2011 00:14, Paul <pchrist wrote:
> "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.
#include <iostream>
struct foo
{
};
void what_am_i(const foo&)
{
std::cout << "I seem to be an lvalue\n";
}
void what_am_i(const foo&&)
{
std::cout << "I seem to be an rvalue\n";
}
int main()
{
foo f;
what_am_i(f);
what_am_i(foo());
}
Outputs:
I seem to be an lvalue
I seem to be an rvalue
Go figure, "noob".
/Leigh
[toc] | [prev] | [next] | [standalone]
| From | Leigh Johnston <leigh@i42.co.uk> |
|---|---|
| Date | 2011-11-20 01:14 +0000 |
| Message-ID | <S8qdnejZWYEMyVXTnZ2dnUVZ8hadnZ2d@giganews.com> |
| In reply to | #11630 |
On 20/11/2011 00:45, Leigh Johnston wrote:
> On 20/11/2011 00:14, Paul <pchrist wrote:
>> "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.
>
> #include <iostream>
>
> struct foo
> {
> };
>
> void what_am_i(const foo&)
> {
> std::cout << "I seem to be an lvalue\n";
> }
>
> void what_am_i(const foo&&)
> {
> std::cout << "I seem to be an rvalue\n";
> }
>
> int main()
> {
> foo f;
> what_am_i(f);
> what_am_i(foo());
> }
>
> Outputs:
>
> I seem to be an lvalue
> I seem to be an rvalue
>
> Go figure, "noob".
Or to put it another way:
#include <iostream>
struct foo
{
};
void what_does_gplusplus_call_me(foo&)
{
std::cout << "I seem to be an lvalue\n";
}
int main()
{
what_does_gplusplus_call_me(foo());
}
Outputs:
test.cpp:14:36: error: invalid initialization of non-const reference of
type 'foo&' from an rvalue of type 'foo'
If the g++ compiler writers are happy calling "foo()" an *rvalue* than
so am I.
/Leigh
[toc] | [prev] | [next] | [standalone]
| From | "Paul" <pchrist<nospam>or@yahoo.co.uk> |
|---|---|
| Date | 2011-11-20 02:34 +0000 |
| Message-ID | <ja9ovj$197t$1@adenine.netfront.net> |
| In reply to | #11631 |
"Leigh Johnston" <leigh@i42.co.uk> wrote in message
news:S8qdnejZWYEMyVXTnZ2dnUVZ8hadnZ2d@giganews.com...
> On 20/11/2011 00:45, Leigh Johnston wrote:
>> On 20/11/2011 00:14, Paul <pchrist wrote:
>>> "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.
>>
>> #include <iostream>
>>
>> struct foo
>> {
>> };
>>
>> void what_am_i(const foo&)
>> {
>> std::cout << "I seem to be an lvalue\n";
>> }
>>
>> void what_am_i(const foo&&)
>> {
>> std::cout << "I seem to be an rvalue\n";
>> }
>>
>> int main()
>> {
>> foo f;
>> what_am_i(f);
>> what_am_i(foo());
>> }
>>
>> Outputs:
>>
>> I seem to be an lvalue
>> I seem to be an rvalue
>>
>> Go figure, "noob".
>
> Or to put it another way:
>
> #include <iostream>
>
> struct foo
> {
> };
>
> void what_does_gplusplus_call_me(foo&)
> {
> std::cout << "I seem to be an lvalue\n";
Are you suggesting a string literal is an l-value?
Seems like a load of confusion to me , its a string literal why not call it
a string literal instead of trying to say its an l-value or an m-value or
whatever.
> }
>
> int main()
> {
> what_does_gplusplus_call_me(foo());
> }
>
> Outputs:
>
> test.cpp:14:36: error: invalid initialization of non-const reference of
> type 'foo&' from an rvalue of type 'foo'
>
> If the g++ compiler writers are happy calling "foo()" an *rvalue* than so
> am I.
>
Basically an l-value is something on the left hand side of an assignment and
an r-value is on the right hand side. Its simple and no need to get involved
in any more complex interpretations of thiis l-value , r-value nonsense
unless you are creating a compiler or something?
Its just the usual arguments revolving around the interpretation of the holy
standard, sorry I mean standardS( for the sake of Ian :) )
I do support you on this and hope you are right because I'd like to see you
correcting that victor guy as he's a bit annoying as all his discussions are
one way. GL, Im not interested in all that l-value , r-value mumbo jumbo
just thought I'd highlight the fact that you corrected victors error,
presuming you are correct. Nobody else responded.
--- Posted via news://freenews.netfront.net/ - Complaints to news@netfront.net ---
[toc] | [prev] | [next] | [standalone]
| From | "Paul" <pchrist<nospam>or@yahoo.co.uk> |
|---|---|
| Date | 2011-11-20 02:44 +0000 |
| Message-ID | <ja9pik$1a5r$1@adenine.netfront.net> |
| In reply to | #11632 |
"Paul or@yahoo.co.uk>" <pchrist<nospam> wrote in message
news:ja9ovj$197t$1@adenine.netfront.net...
>
> "Leigh Johnston" <leigh@i42.co.uk> wrote in message
> news:S8qdnejZWYEMyVXTnZ2dnUVZ8hadnZ2d@giganews.com...
>> On 20/11/2011 00:45, Leigh Johnston wrote:
>>> On 20/11/2011 00:14, Paul <pchrist wrote:
>>>> "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.
>>>
>>> #include <iostream>
>>>
>>> struct foo
>>> {
>>> };
>>>
>>> void what_am_i(const foo&)
>>> {
>>> std::cout << "I seem to be an lvalue\n";
>>> }
>>>
>>> void what_am_i(const foo&&)
>>> {
>>> std::cout << "I seem to be an rvalue\n";
>>> }
>>>
>>> int main()
>>> {
>>> foo f;
>>> what_am_i(f);
>>> what_am_i(foo());
>>> }
>>>
>>> Outputs:
>>>
>>> I seem to be an lvalue
>>> I seem to be an rvalue
>>>
>>> Go figure, "noob".
>>
>> Or to put it another way:
>>
>> #include <iostream>
>>
>> struct foo
>> {
>> };
>>
>> void what_does_gplusplus_call_me(foo&)
>> {
>> std::cout << "I seem to be an lvalue\n";
>
> Are you suggesting a string literal is an l-value?
>
> Seems like a load of confusion to me , its a string literal why not call
> it a string literal instead of trying to say its an l-value or an m-value
> or whatever.
>
>
>> }
>>
>> int main()
>> {
>> what_does_gplusplus_call_me(foo());
>> }
>>
>> Outputs:
>>
>> test.cpp:14:36: error: invalid initialization of non-const reference of
>> type 'foo&' from an rvalue of type 'foo'
>>
>> If the g++ compiler writers are happy calling "foo()" an *rvalue* than so
>> am I.
>>
>
> Basically an l-value is something on the left hand side of an assignment
> and an r-value is on the right hand side. Its simple and no need to get
> involved in any more complex interpretations of thiis l-value , r-value
> nonsense unless you are creating a compiler or something?
> Its just the usual arguments revolving around the interpretation of the
> holy standard, sorry I mean standardS( for the sake of Ian :) )
>
> I do support you on this and hope you are right because I'd like to see
> you correcting that victor guy as he's a bit annoying as all his
> discussions are one way. GL, Im not interested in all that l-value ,
> r-value mumbo jumbo just thought I'd highlight the fact that you corrected
> victors error, presuming you are correct. Nobody else responded.
>
"discussions" is perhaps not the correct term for victors postings. Anyway
this level of intelligence is beyond your conversational skills, go back to
making coffee. I'll have one sugar and milk.
--- Posted via news://freenews.netfront.net/ - Complaints to news@netfront.net ---
[toc] | [prev] | [next] | [standalone]
| From | Leigh Johnston <leigh@i42.co.uk> |
|---|---|
| Date | 2011-11-20 13:06 +0000 |
| Message-ID | <Zt-dnYjTWOLxZlXTnZ2dnUVZ7o2dnZ2d@giganews.com> |
| In reply to | #11632 |
On 20/11/2011 02:34, Paul <pchrist wrote:
> "Leigh Johnston"<leigh@i42.co.uk> wrote in message
> news:S8qdnejZWYEMyVXTnZ2dnUVZ8hadnZ2d@giganews.com...
>> On 20/11/2011 00:45, Leigh Johnston wrote:
>>> On 20/11/2011 00:14, Paul<pchrist wrote:
>>>> "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.
>>>
>>> #include<iostream>
>>>
>>> struct foo
>>> {
>>> };
>>>
>>> void what_am_i(const foo&)
>>> {
>>> std::cout<< "I seem to be an lvalue\n";
>>> }
>>>
>>> void what_am_i(const foo&&)
>>> {
>>> std::cout<< "I seem to be an rvalue\n";
>>> }
>>>
>>> int main()
>>> {
>>> foo f;
>>> what_am_i(f);
>>> what_am_i(foo());
>>> }
>>>
>>> Outputs:
>>>
>>> I seem to be an lvalue
>>> I seem to be an rvalue
>>>
>>> Go figure, "noob".
>>
>> Or to put it another way:
>>
>> #include<iostream>
>>
>> struct foo
>> {
>> };
>>
>> void what_does_gplusplus_call_me(foo&)
>> {
>> std::cout<< "I seem to be an lvalue\n";
>
> Are you suggesting a string literal is an l-value?
No; a string literal is an rvalue. The function
what_does_gplusplus_call_me can only accept an lvalue as an argument
(hence the std::cout statement) as a non-const lvalue reference will not
bind to an rvalue hence the explanatory compiler error below.
>
> Seems like a load of confusion to me , its a string literal why not call it
> a string literal instead of trying to say its an l-value or an m-value or
> whatever.
It is an rvalue; the confusion is yours due to your inability to read
posts properly.
>
>
>> }
>>
>> int main()
>> {
>> what_does_gplusplus_call_me(foo());
>> }
>>
>> Outputs:
>>
>> test.cpp:14:36: error: invalid initialization of non-const reference of
>> type 'foo&' from an rvalue of type 'foo'
>>
>> If the g++ compiler writers are happy calling "foo()" an *rvalue* than so
>> am I.
>>
>
> Basically an l-value is something on the left hand side of an assignment and
> an r-value is on the right hand side. Its simple and no need to get involved
> in any more complex interpretations of thiis l-value , r-value nonsense
> unless you are creating a compiler or something?
> Its just the usual arguments revolving around the interpretation of the holy
> standard, sorry I mean standardS( for the sake of Ian :) )
Again you appear unable to accept the fact that the same term can have a
different and/or enhanced meaning from one domain to the next. C++ is
not Computer Science; Computer Science is not C++.
foo() = foo(); // even though foo() is an rvalue I can still assign to
it as foo is of class type; so in C++ saying an rvalue as something
always on the "right hand side of an assignment" is erroneous.
>
> I do support you on this and hope you are right because I'd like to see you
> correcting that victor guy as he's a bit annoying as all his discussions are
> one way. GL, Im not interested in all that l-value , r-value mumbo jumbo
> just thought I'd highlight the fact that you corrected victors error,
> presuming you are correct. Nobody else responded.
lvalues and rvalues are not "mumbo jumbo"; understanding them is key to
using C++ (especially C++11) properly.
HTH.
/Leigh
[toc] | [prev] | [next] | [standalone]
| From | Leigh Johnston <leigh@i42.co.uk> |
|---|---|
| Date | 2011-11-20 13:21 +0000 |
| Message-ID | <l6Odnf_e2fxnY1XTnZ2dnUVZ7s2dnZ2d@giganews.com> |
| In reply to | #11637 |
On 20/11/2011 13:06, Leigh Johnston wrote:
> On 20/11/2011 02:34, Paul <pchrist wrote:
>> "Leigh Johnston"<leigh@i42.co.uk> wrote in message
>> news:S8qdnejZWYEMyVXTnZ2dnUVZ8hadnZ2d@giganews.com...
>>> On 20/11/2011 00:45, Leigh Johnston wrote:
>>>> On 20/11/2011 00:14, Paul<pchrist wrote:
>>>>> "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.
>>>>
>>>> #include<iostream>
>>>>
>>>> struct foo
>>>> {
>>>> };
>>>>
>>>> void what_am_i(const foo&)
>>>> {
>>>> std::cout<< "I seem to be an lvalue\n";
>>>> }
>>>>
>>>> void what_am_i(const foo&&)
>>>> {
>>>> std::cout<< "I seem to be an rvalue\n";
>>>> }
>>>>
>>>> int main()
>>>> {
>>>> foo f;
>>>> what_am_i(f);
>>>> what_am_i(foo());
>>>> }
>>>>
>>>> Outputs:
>>>>
>>>> I seem to be an lvalue
>>>> I seem to be an rvalue
>>>>
>>>> Go figure, "noob".
>>>
>>> Or to put it another way:
>>>
>>> #include<iostream>
>>>
>>> struct foo
>>> {
>>> };
>>>
>>> void what_does_gplusplus_call_me(foo&)
>>> {
>>> std::cout<< "I seem to be an lvalue\n";
>>
>> Are you suggesting a string literal is an l-value?
>
> No; a string literal is an rvalue. The function
> what_does_gplusplus_call_me can only accept an lvalue as an argument
> (hence the std::cout statement) as a non-const lvalue reference will not
> bind to an rvalue hence the explanatory compiler error below.
Actually I am mistaken; a string literal is an lvalue.
/Leigh
[toc] | [prev] | [next] | [standalone]
| From | "Paul" <pchrist<nospam>or@yahoo.co.uk> |
|---|---|
| Date | 2011-11-20 16:51 +0000 |
| Message-ID | <jabb66$319i$1@adenine.netfront.net> |
| In reply to | #11638 |
"Leigh Johnston" <leigh@i42.co.uk> wrote in message
news:l6Odnf_e2fxnY1XTnZ2dnUVZ7s2dnZ2d@giganews.com...
> On 20/11/2011 13:06, Leigh Johnston wrote:
>> On 20/11/2011 02:34, Paul <pchrist wrote:
>>> "Leigh Johnston"<leigh@i42.co.uk> wrote in message
>>> news:S8qdnejZWYEMyVXTnZ2dnUVZ8hadnZ2d@giganews.com...
>>>> On 20/11/2011 00:45, Leigh Johnston wrote:
>>>>> On 20/11/2011 00:14, Paul<pchrist wrote:
>>>>>> "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.
>>>>>
>>>>> #include<iostream>
>>>>>
>>>>> struct foo
>>>>> {
>>>>> };
>>>>>
>>>>> void what_am_i(const foo&)
>>>>> {
>>>>> std::cout<< "I seem to be an lvalue\n";
>>>>> }
>>>>>
>>>>> void what_am_i(const foo&&)
>>>>> {
>>>>> std::cout<< "I seem to be an rvalue\n";
>>>>> }
>>>>>
>>>>> int main()
>>>>> {
>>>>> foo f;
>>>>> what_am_i(f);
>>>>> what_am_i(foo());
>>>>> }
>>>>>
>>>>> Outputs:
>>>>>
>>>>> I seem to be an lvalue
>>>>> I seem to be an rvalue
>>>>>
>>>>> Go figure, "noob".
>>>>
>>>> Or to put it another way:
>>>>
>>>> #include<iostream>
>>>>
>>>> struct foo
>>>> {
>>>> };
>>>>
>>>> void what_does_gplusplus_call_me(foo&)
>>>> {
>>>> std::cout<< "I seem to be an lvalue\n";
>>>
>>> Are you suggesting a string literal is an l-value?
>>
>> No; a string literal is an rvalue. The function
>> what_does_gplusplus_call_me can only accept an lvalue as an argument
>> (hence the std::cout statement) as a non-const lvalue reference will not
>> bind to an rvalue hence the explanatory compiler error below.
>
> Actually I am mistaken; a string literal is an lvalue.
>
You don't seem very sure about this.
--- Posted via news://freenews.netfront.net/ - Complaints to news@netfront.net ---
[toc] | [prev] | [next] | [standalone]
| From | Leigh Johnston <leigh@i42.co.uk> |
|---|---|
| Date | 2011-11-20 17:24 +0000 |
| Message-ID | <X7OdnYwCHc15qlTTnZ2dnUVZ8qqdnZ2d@giganews.com> |
| In reply to | #11641 |
On 20/11/2011 16:51, Paul <pchrist wrote:
> "Leigh Johnston"<leigh@i42.co.uk> wrote in message
> news:l6Odnf_e2fxnY1XTnZ2dnUVZ7s2dnZ2d@giganews.com...
>> On 20/11/2011 13:06, Leigh Johnston wrote:
>>> On 20/11/2011 02:34, Paul<pchrist wrote:
>>>> "Leigh Johnston"<leigh@i42.co.uk> wrote in message
>>>> news:S8qdnejZWYEMyVXTnZ2dnUVZ8hadnZ2d@giganews.com...
>>>>> On 20/11/2011 00:45, Leigh Johnston wrote:
>>>>>> On 20/11/2011 00:14, Paul<pchrist wrote:
>>>>>>> "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.
>>>>>>
>>>>>> #include<iostream>
>>>>>>
>>>>>> struct foo
>>>>>> {
>>>>>> };
>>>>>>
>>>>>> void what_am_i(const foo&)
>>>>>> {
>>>>>> std::cout<< "I seem to be an lvalue\n";
>>>>>> }
>>>>>>
>>>>>> void what_am_i(const foo&&)
>>>>>> {
>>>>>> std::cout<< "I seem to be an rvalue\n";
>>>>>> }
>>>>>>
>>>>>> int main()
>>>>>> {
>>>>>> foo f;
>>>>>> what_am_i(f);
>>>>>> what_am_i(foo());
>>>>>> }
>>>>>>
>>>>>> Outputs:
>>>>>>
>>>>>> I seem to be an lvalue
>>>>>> I seem to be an rvalue
>>>>>>
>>>>>> Go figure, "noob".
>>>>>
>>>>> Or to put it another way:
>>>>>
>>>>> #include<iostream>
>>>>>
>>>>> struct foo
>>>>> {
>>>>> };
>>>>>
>>>>> void what_does_gplusplus_call_me(foo&)
>>>>> {
>>>>> std::cout<< "I seem to be an lvalue\n";
>>>>
>>>> Are you suggesting a string literal is an l-value?
>>>
>>> No; a string literal is an rvalue. The function
>>> what_does_gplusplus_call_me can only accept an lvalue as an argument
>>> (hence the std::cout statement) as a non-const lvalue reference will not
>>> bind to an rvalue hence the explanatory compiler error below.
>>
>> Actually I am mistaken; a string literal is an lvalue.
>>
> You don't seem very sure about this.
I am sure; the Standard explicitly says that a string literal is an
lvalue but this is a side discussion as it has nothing to do with what
foo& can bind to.
/Leigh
[toc] | [prev] | [next] | [standalone]
| From | SG <s.gesemann@gmail.com> |
|---|---|
| Date | 2011-11-20 00:49 -0800 |
| Message-ID | <f24e5c0b-dc6a-4f78-b387-aa52bca6dfcf@y42g2000yqh.googlegroups.com> |
| In reply to | #11619 |
On Nov 19, 2:45 pm, Victor Bazarov wrote: > 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. You seem to confuse two concepts, (1) value category and (2) whether or not an expression refers to an object. There is some correlation between these two but no equivalence. What is true though is that every lvalue (of object type) refers to an object. But T() is always an rvalue expression no matter what kind of object type T is (scalar or not). Let me quote part of 3.10 of the current C++ ISO standard: "An rvalue (...) is an xvalue, a temporary object or a subobject thereof, or a value that is not associated with an object." So, saying that A() is an rvalue which refers to a temporary object is not a contradiction. In the former version of the standard it was also clarified when an rvalue (that is not an xvalue) actually refers to an object and when not. I can't find it in the current incarnation but I guess it did not change. PRvalues of _scalar_types_ are not associated with an object, they are just "values". And since int is a scalar type int() does not refer to an object (temporary or otherwise). But of course, a reference of object type has to refer to an object. That's why a temporary object is created as part of the reference initialization procedure. I suggest to look up §3.10 and the part about reference initialization in both versions of the C++ ISO standard. Cheers! SG
[toc] | [prev] | [next] | [standalone]
| From | Victor Bazarov <v.bazarov@comcast.invalid> |
|---|---|
| Date | 2011-11-19 08:42 -0500 |
| Message-ID | <ja8bo7$88k$1@dont-email.me> |
| In reply to | #11616 |
On 11/19/2011 7:33 AM, 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.
>
> Temporaries are mutable, so in whole my snippet is legal C++. Am i correct?
AIUI, your code has undefined behavior. 'c' is bound to an rvalue, and
using 'const_cast' to produce an lvalue out of it is not among the
allowed operations for 'const_cast'.
V
--
I do not respond to top-posted replies, please don't ask
[toc] | [prev] | [next] | [standalone]
| From | Paavo Helde <myfirstname@osa.pri.ee> |
|---|---|
| Date | 2011-11-19 08:20 -0600 |
| Message-ID | <Xns9FA2A6514BCDmyfirstnameosapriee@216.196.109.131> |
| In reply to | #11618 |
Victor Bazarov <v.bazarov@comcast.invalid> wrote in
news:ja8bo7$88k$1@dont-email.me:
> On 11/19/2011 7:33 AM, 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.
>>
>> Temporaries are mutable, so in whole my snippet is legal C++. Am i
>> correct?
>
> AIUI, your code has undefined behavior. 'c' is bound to an rvalue,
> and using 'const_cast' to produce an lvalue out of it is not among the
> allowed operations for 'const_cast'.
The lvalue and rvalue notions apply to source code expressions (3.10/1:
"Every expression is either an lvalue or an rvalue"). Expression 'c' is
of a reference type, which means it is a lvalue, doesn't it? (rvalue
references use && syntax). And lvalue->lvalue conversion is well defined
for const_cast.
Or have I misunderstood something?
Cheers
Paavo
[toc] | [prev] | [next] | [standalone]
| From | Victor Bazarov <v.bazarov@comcast.invalid> |
|---|---|
| Date | 2011-11-19 15:51 -0500 |
| Message-ID | <ja94s6$61c$1@dont-email.me> |
| In reply to | #11621 |
On 11/19/2011 9:20 AM, Paavo Helde wrote:
> Victor Bazarov<v.bazarov@comcast.invalid> wrote in
> news:ja8bo7$88k$1@dont-email.me:
>
>> On 11/19/2011 7:33 AM, 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.
>>>
>>> Temporaries are mutable, so in whole my snippet is legal C++. Am i
>>> correct?
>>
>> AIUI, your code has undefined behavior. 'c' is bound to an rvalue,
>> and using 'const_cast' to produce an lvalue out of it is not among the
>> allowed operations for 'const_cast'.
>
> The lvalue and rvalue notions apply to source code expressions (3.10/1:
> "Every expression is either an lvalue or an rvalue"). Expression 'c' is
> of a reference type, which means it is a lvalue, doesn't it? (rvalue
> references use&& syntax). And lvalue->lvalue conversion is well defined
> for const_cast.
>
> Or have I misunderstood something?
int() is an rvalue expression. Now, explain that.
V
--
I do not respond to top-posted replies, please don't ask
[toc] | [prev] | [next] | [standalone]
| From | Paavo Helde <myfirstname@osa.pri.ee> |
|---|---|
| Date | 2011-11-19 16:51 -0600 |
| Message-ID | <Xns9FA38B6B39D2myfirstnameosapriee@216.196.109.131> |
| In reply to | #11624 |
Victor Bazarov <v.bazarov@comcast.invalid> wrote in news:ja94s6$61c$1
@dont-email.me:
> On 11/19/2011 9:20 AM, Paavo Helde wrote:
>> Victor Bazarov<v.bazarov@comcast.invalid> wrote in
>> news:ja8bo7$88k$1@dont-email.me:
>>
>>> On 11/19/2011 7:33 AM, 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.
>>>>
>>>> Temporaries are mutable, so in whole my snippet is legal C++. Am i
>>>> correct?
>>>
>>> AIUI, your code has undefined behavior. 'c' is bound to an rvalue,
>>> and using 'const_cast' to produce an lvalue out of it is not among
the
>>> allowed operations for 'const_cast'.
>>
>> The lvalue and rvalue notions apply to source code expressions
(3.10/1:
>> "Every expression is either an lvalue or an rvalue"). Expression 'c'
is
>> of a reference type, which means it is a lvalue, doesn't it? (rvalue
>> references use&& syntax). And lvalue->lvalue conversion is well
defined
>> for const_cast.
>>
>> Or have I misunderstood something?
>
> int() is an rvalue expression. Now, explain that.
So is A() (where A is a class type for example), so by that logic it
would not be possible to create mutable temporaries at all.
In the OP-s example the const_cast was not applied to int(). This would
be indeed an error (Comeau agrees:)
"ComeauTest.c", line 2: error: expression must be an lvalue
int& m = const_cast<int&>(int());
const_cast was applied to a reference instead. Reference always points to
an object; yes, this may be some temporary object initialized by an
rvalue, but so what? Casting away constness would be illegal if the
object is const (as suggested by Johannes), but this is another topic and
not related to rvalues at all.
Cheers
Paavo
[toc] | [prev] | [next] | [standalone]
| From | "Paul" <pchrist<nospam>or@yahoo.co.uk> |
|---|---|
| Date | 2011-11-20 00:23 +0000 |
| Message-ID | <ja9h9v$t2t$1@adenine.netfront.net> |
| In reply to | #11624 |
"Victor Bazarov" <v.bazarov@comcast.invalid> wrote in message
news:ja94s6$61c$1@dont-email.me...
> On 11/19/2011 9:20 AM, Paavo Helde wrote:
>> Victor Bazarov<v.bazarov@comcast.invalid> wrote in
>> news:ja8bo7$88k$1@dont-email.me:
>>
>>> On 11/19/2011 7:33 AM, 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.
>>>>
>>>> Temporaries are mutable, so in whole my snippet is legal C++. Am i
>>>> correct?
>>>
>>> AIUI, your code has undefined behavior. 'c' is bound to an rvalue,
>>> and using 'const_cast' to produce an lvalue out of it is not among the
>>> allowed operations for 'const_cast'.
>>
>> The lvalue and rvalue notions apply to source code expressions (3.10/1:
>> "Every expression is either an lvalue or an rvalue"). Expression 'c' is
>> of a reference type, which means it is a lvalue, doesn't it? (rvalue
>> references use&& syntax). And lvalue->lvalue conversion is well defined
>> for const_cast.
>>
>> Or have I misunderstood something?
>
> int() is an rvalue expression. Now, explain that.
>
What is it about "int()" that you do not understand exactly?
--- Posted via news://freenews.netfront.net/ - Complaints to news@netfront.net ---
[toc] | [prev] | [next] | [standalone]
| From | Johannes Schaub <schaub.johannes@googlemail.com> |
|---|---|
| Date | 2011-11-19 16:16 +0100 |
| Message-ID | <ja8dkh$snr$1@news.albasani.net> |
| In reply to | #11616 |
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.
So in your case the reference refers to an object of type "const int" - you
are not allowed to modify it.
[toc] | [prev] | [next] | [standalone]
Page 1 of 2 [1] 2 Next page →
Back to top | Article view | comp.lang.c++
csiph-web