Re: Mutability of temporary variables
"Johannes Schaub" <schaub.johannes@googlemail.com> wrote in message
news:ja8dkh$snr$1@news.albasani.net...
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.
"int()" is not a value. Its a piece of source code that represents
construction of an integer data type.
Whether the data is temporary or non temporary depends how far into the
compilation process you go.
What do you mean by temporary?temprorarily created in the compilation
process, or temporarily created in the programs executable process?
temporarily created in the cpu's translation process ?
wtf is temporary supposed to mean?
--- Posted via news://freenews.netfront.net/ - Complaints to news@netfront.net ---