473,327 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Temporaries, non-const yet r-value?!

Firstly, we all know that temporaries are non-const, which I shall
demonstrate with the following code:

struct Blah
{
int monkey;

void SetMonkey(int const supplied)
{
monkey = supplied;
}
};
Blah SomeFunc()
{
return Blah();
}
int main()
{
Blah().SetMonkey(5);

SomeFunc().monkey = 5;
}
But, at the same time, temporaries are "r-value"'s. The following is
illegal:

int Blah()
{
return 5;
}

int main
{
Blah() = 6;
}
Also, in the previous code, "Blah().monkey = 5" is illegal.
It's not legal to bind a temporary to a non-const reference, because it is
an "r-value".
You're allowed to use "const_cast" to cast away the constness if the
original object was in fact non-const...

struct Blah
{
int a;
double b;
char c;
float** p_p_f;
wchar_t k;
bool f;
};

int main()
{
Blah const &poo = Blah();

Blah &cow = const_cast<Blah&>(poo);

cow.a = 5;
}

Any thoughts on this?
-JKop
Jul 22 '05 #1
10 1521
* JKop:

Any thoughts on this?


It's a correct observation. And yes it's problematic. The Mojo article
that (now long ago, measured in software development time) started the
current debate:
<url: http://www.cuj.com/documents/s=8246/cujcexp2102alexandr/alexandr.htm>.

And recently in this forum Chris Theis raised the question of what's
happened since then.

That's a new thread NRVO in [comp.std.c++], but very little concrete
has been forthcoming.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #2
JKop wrote in news:wL*******************@news.indigo.ie in comp.lang.c++:
You're allowed to use "const_cast" to cast away the constness if the
original object was in fact non-const...

Thats correct, but ...
struct Blah
{ };

int main()
{
Blah const &poo = Blah();
The problem is here, the rvalue is allowed to bind via a temporary
and although we all know that the temp will in reality (*) be no
more constant than the rvalue, the standard allows it to be.

*) Here: reality == Desktop PC's

So the actual object "poo" refers to is a real constant.

Blah &cow = const_cast<Blah&>(poo);
That is OK, but ...

cow.a = 5;
That is UB.
}


Microsofts VC++ has a mode where it check's (at runtime) for
buffer overflows, such a system could also be used to check for
the constantness of the temp "poo" was bound too, and your code
nolonger works.

Also in this case, since the "Blah" that was used is a value
initialized POD, the constant could actualy be in read-only
memory, bang goes your code again (or maybe it doesn't compile).

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #3
The problem is here, the rvalue is allowed to bind via a temporary
and although we all know that the temp will in reality (*) be no
more constant than the rvalue, the standard allows it to be.
No it does not. Temporaries are not necessarily const. A temporary
of class type can be modified.

That is OK, but ...

cow.a = 5;

That is UB.

No.
Also in this case, since the "Blah" that was used is a value
initialized POD, the constant could actualy be in read-only
memory, bang goes your code again (or maybe it doesn't compile).


No.
Jul 22 '05 #4
Ron Natalie wrote in news:41***********************@news.newshosting.co m in
comp.lang.c++:
The problem is here, the rvalue is allowed to bind via a temporary
and although we all know that the temp will in reality (*) be no
more constant than the rvalue, the standard allows it to be.
No it does not.


Your right it doesn't allow it, it *requires* it:

8.5.3/5:

The relevent bit:

— A temporary of type “cv1 T2” [sic] is created, and a constructor is
called to copy the entire rvalue object into the temporary. The
reference is bound to the temporary or to a sub-object within the
temporary.93)
Temporaries are not necessarily const. A temporary
of class type can be modified.
No. No.


Yes, Yes.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #5
Rob Williscroft wrote:
Ron Natalie wrote in news:41***********************@news.newshosting.co m in
comp.lang.c++:

The problem is here, the rvalue is allowed to bind via a temporary
and although we all know that the temp will in reality (*) be no
more constant than the rvalue, the standard allows it to be.
No it does not.

Your right it doesn't allow it, it *requires* it:


Did you miss the "one of the following ways (the choice is implementation-
defined" a few lines above the quoted section?

8.5.3/5:

The relevent bit:

— A temporary of type “cv1 T2” [sic] is created, and a constructor is
called to copy the entire rvalue object into the temporary. The
reference is bound to the temporary or to a sub-object within the
temporary.93)

Temporaries are not necessarily const. A temporary
of class type can be modified.

No.


No.

Yes, Yes.


NO NO

Rob.


Victor
Jul 22 '05 #6
Victor Bazarov wrote in
news:ay****************@newsread1.dllstx09.us.to.v erio.net in
comp.lang.c++:
The problem is here, the rvalue is allowed to bind via a temporary
and although we all know that the temp will in reality (*) be no
more constant than the rvalue, the standard allows it to be.

No it does not.

Your right it doesn't allow it, it *requires* it:


Did you miss the "one of the following ways (the choice is
implementation- defined" a few lines above the quoted section?


Thats " ... the rvalue is alowed to bind via a temporary ... " as I
said above, but if it does the temporary is *required* to be const.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #7
Rob Williscroft wrote:
Victor Bazarov wrote in
news:ay****************@newsread1.dllstx09.us.to.v erio.net in
comp.lang.c++:

>The problem is here, the rvalue is allowed to bind via a temporary
>and although we all know that the temp will in reality (*) be no
>more constant than the rvalue, the standard allows it to be.

No it does not.
Your right it doesn't allow it, it *requires* it:


Did you miss the "one of the following ways (the choice is
implementation- defined" a few lines above the quoted section?

Thats " ... the rvalue is alowed to bind via a temporary ... " as I
said above, but if it does the temporary is *required* to be const.


Ah, sorry, I didn't understand what you were hinting at. Yes, if the
temporary _is_ created, that temporary will be const. It doesn't have
to be created, though.

So, using the original example

Blah const &poo = Blah();
Blah &cow = const_cast<Blah&>(poo);
cow.a = 5;

the behaviour is NOT undefined, but rather _implementation_defined_. That
is because the 'poo' reference _may_ bind to the original temporary and
not to an intermediate one.

Victor
Jul 22 '05 #8
Victor Bazarov wrote in
news:iT****************@newsread1.dllstx09.us.to.v erio.net in
comp.lang.c++:
Thats " ... the rvalue is alowed to bind via a temporary ... " as I
said above, but if it does the temporary is *required* to be const.


Ah, sorry, I didn't understand what you were hinting at. Yes, if the
temporary _is_ created, that temporary will be const. It doesn't have
to be created, though.

So, using the original example

Blah const &poo = Blah();
Blah &cow = const_cast<Blah&>(poo);
cow.a = 5;

the behaviour is NOT undefined, but rather _implementation_defined_.
That is because the 'poo' reference _may_ bind to the original
temporary and not to an intermediate one.


Yes, but what is implementation defined here is wether UB happens
or not. IMO that is UB (the same as n * 0 == 0 for any n). YMMV.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #9

"Rob Williscroft" <rt*@freenet.co.uk> wrote in message
news:Xn**********************************@130.133. 1.4...
Victor Bazarov wrote in
news:iT****************@newsread1.dllstx09.us.to.v erio.net in
comp.lang.c++:
Thats " ... the rvalue is alowed to bind via a temporary ... " as I
said above, but if it does the temporary is *required* to be const.


Ah, sorry, I didn't understand what you were hinting at. Yes, if the
temporary _is_ created, that temporary will be const. It doesn't have
to be created, though.

So, using the original example

Blah const &poo = Blah();
Blah &cow = const_cast<Blah&>(poo);
cow.a = 5;

the behaviour is NOT undefined, but rather _implementation_defined_.
That is because the 'poo' reference _may_ bind to the original
temporary and not to an intermediate one.


Yes, but what is implementation defined here is wether UB happens
or not. IMO that is UB (the same as n * 0 == 0 for any n). YMMV.


But is UB * 0 always == 0 ???

-:)

-Mike

Jul 22 '05 #10
Mike Wahler wrote in news:9nbfd.6949$KJ6.5245
@newsread1.news.pas.earthlink.net in comp.lang.c++:

"Rob Williscroft" <rt*@freenet.co.uk> wrote in message
news:Xn**********************************@130.133. 1.4...
Victor Bazarov wrote in
news:iT****************@newsread1.dllstx09.us.to.v erio.net in
comp.lang.c++:
>> Thats " ... the rvalue is alowed to bind via a temporary ... " as I
>> said above, but if it does the temporary is *required* to be const.
>
> Ah, sorry, I didn't understand what you were hinting at. Yes, if
> the temporary _is_ created, that temporary will be const. It
> doesn't have to be created, though.
>
> So, using the original example
>
> Blah const &poo = Blah();
> Blah &cow = const_cast<Blah&>(poo);
> cow.a = 5;
>
> the behaviour is NOT undefined, but rather _implementation_defined_.
> That is because the 'poo' reference _may_ bind to the original
> temporary and not to an intermediate one.
>


Yes, but what is implementation defined here is wether UB happens
or not. IMO that is UB (the same as n * 0 == 0 for any n). YMMV.


But is UB * 0 always == 0 ???

-:)


Yep I think so:

void f()
{
char *s = new char[10];
delete s;
}

int main()
{
}

f() exhibts UB but its called 0 times so the programme
doesn't exhibit UB.

<SCNR mode="daft-as-brush">

Of course UB is calculated using signed maginitude and we have
+ve and -ve 0;

implementation-defined * UB == UB ( greater-than-0 * -0 == -0 ) (*)

and

UB * don't-do-it-it-hurts == don't-do-it-it-hurts ( -0 * +0 == +0 ) (*).

*) Note: I've no idea if signed magnitude CPU's actualy work this way :)

</SCNR>

:-)

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: JKop | last post by:
The following compiles: // syrup.cpp struct DoubleInDisguise { double data; };
13
by: S.Tobias | last post by:
I'm examining the existence of temporary objects by looking at their addresses. The trick is to create a structure that contains an array as its first member. In an expression the array rvalue...
10
by: ATASLO | last post by:
In the following example, section #3 fails under VC98, VC2003, VC2005 Express Beta (Aug 2004) and g++ 3.3.2. Is this just a pitfall of the C++ specification? Why don't any of the above compilers...
3
by: bb | last post by:
Hi, Have a query regarding the life of temporaries. Here is the code... class MyNumber { public: MyNumber(int n) : n(n) { cout << "Object Constructed." << endl; }
12
by: dave_dp | last post by:
Hi, I have just started learning C++ language.. I've read much even tried to understand the way standard says but still can't get the grasp of that concept. When parameters are passed/returned...
6
by: wizwx | last post by:
Is there anything wrong with the following code? class A { ... }; class B : public A { ... }; // definitions of class A and B, these are OK Foo() { A & a = B(); // ?? A * p = &B(); // ??...
0
by: Victor Bazarov | last post by:
aitorf666@gmail.com wrote: My bet is that it's not going to happen. Too late. Many compilers have already implemented the && syntax. Besides, would you allow the keyword 'mutable' to be used...
0
by: Jerry Coffin | last post by:
In article <9f60e411-a5b1-4571-9d3d-005432378cd4@ 56g2000hsm.googlegroups.com>, aitorf666@gmail.com says... That's not the real reason for rvalue references. There are two primary reasons for...
9
by: usao | last post by:
Does anyone have an example of how to create a class which describes and array, such that I can use the subscript operator on both the left and right side of an assignment statement? This is as...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.