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 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?
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/ 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.
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/
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
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/
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
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/
"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
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/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: JKop |
last post by:
The following compiles:
// syrup.cpp
struct DoubleInDisguise
{
double data;
};
|
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...
|
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...
|
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;
}
|
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...
|
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(); // ??...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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...
| | |