Re: Non-const, L-value temporary
* Frederick Gotham:
I think I have a way of producing a non-const, L-value temporary.
Relevant information:
(1) A name-less, temporary object is non-const (unless explicitly
defined as const).
(2) const_cast can be used legitimately to cast away constness from a
pointer or reference in order to alter a non-const object.
(3) A const reference is an L-value.
And thus I give you:
int main()
{
const_cast<int&>( static_cast<const int&>( int() ) ) = 5;
}
Three questions:
(1) Is the program well-formed?
(2) Does the program invoke undefined behaviour?
(3) Do I really have a non-const, L-value temporary?
I don't know. The reason this isn't straightforward is that it's
trivial to produce a guaranteed OK non-const temporary lvalue for a
class type object. Just provide conversion to T&.
However, you're posting to the wrong group: questions about possible
deficiencies in the language definition (such as, shouldn't we simply
get rid of the rvalue concept?) belong in [comp.std.c++].
Questions of practical usage belong here, but there's no way the above
constitues something anybody (sane, that is) would use.
--
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?
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]