Topic: Defect Report: lvalue-to-rvalue conversion incorrectly doesn't apply to rvalue arrays
Author: "Johannes Schaub (litb)" <schaub-johannes@web.de>
Date: Mon, 12 Oct 2009 10:23:57 CST Raw View
5[expr]/8 contains the following wording
Whenever an lvalue expression appears as an operand of an operator that
expects an rvalue for that operand, the lvalue-to-rvalue (4.1), array-to-
pointer (4.2), or function-to-pointer (4.3) standard conversions are applied
to convert the expression to an rvalue.
This won't cause the conversion for rvalue arrays. Testcase that i believe
is currently rejected by the standards wording (but should be accepted):
struct A { int n[1]; };
A f() { return A(); }
int main() { f().n[0] = 0; }
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: tohava <tohava@gmail.com>
Date: Mon, 12 Oct 2009 13:14:07 CST Raw View
On Oct 12, 6:23 pm, "Johannes Schaub (litb)" <schaub-johan...@web.de>
wrote:
> This won't cause the conversion for rvalue arrays. Testcase that i believe
> is currently rejected by the standards wording (but should be accepted):
>
> struct A { int n[1]; };
> A f() { return A(); }
> int main() { f().n[0] = 0; }
While I agree that the wording might need a change, I do not think
this should be supported. The reason for this to stay consistent with
the fact that temporaries can be passed via references only if they
are constant. To be consistent with this, I believe that maybe (I
stress the maybe here...)
struct A { mutable int n[1]; };
A f() { return A(); }
int main() { f().n[0] = 0; }
should work, but that your code should not.
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: "Johannes Schaub (litb)" <schaub-johannes@web.de>
Date: Tue, 13 Oct 2009 11:56:07 CST Raw View
tohava wrote:
> On Oct 12, 6:23 pm, "Johannes Schaub (litb)" <schaub-johan...@web.de>
> wrote:
>> This won't cause the conversion for rvalue arrays. Testcase that i
>> believe is currently rejected by the standards wording (but should be
>> accepted):
>>
>> struct A { int n[1]; };
>> A f() { return A(); }
>> int main() { f().n[0] = 0; }
>
> While I agree that the wording might need a change, I do not think
> this should be supported. The reason for this to stay consistent with
> the fact that temporaries can be passed via references only if they
> are constant. To be consistent with this, I believe that maybe (I
> stress the maybe here...)
>
> struct A { mutable int n[1]; };
> A f() { return A(); }
> int main() { f().n[0] = 0; }
>
> should work, but that your code should not.
>
I'm sorry about my poor testcase. I was accidentally not showing the precise
problem at hand. I will think better next time to come up with something
having immediately to do with the problem at hand. The following is a better
example showing the problem:
struct A { int n[1]; };
A f() { return A(); }
int main() { f().n + 0; }
You will have a hard time to forbid changing something in a class-type
rvalue like in the example though. In our case, we have to do with a non-
const rvalue - allowing to change it is a core feature of move semantics.
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]