Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

#Actualsamoth

Posted 02 August 2012 - 05:45 AM

And what does const really do, i mean what is it really other that making something constant? What does it allow me to do?

The const qualifier does mainly two things:
1. It is a promise (not more than that) to the compiler that you will not modify the value. You can still cast the const away and modify the value anyway, but the compiler will assume that you keep your promise and might make optimizations based this, so cheating is unwise.
2. It allows you to bind a reference to a temporary, and extends the lifetime of that temporary to the lifetime of the const reference. Otherwise, if you were allowed to bind to a temporary (say, returning a local variable by reference) then whatever you alias does not exist any more by the time you use it, which is bad mojo. You would be reading and possibly modifying some more or less arbitrary data on the stack which might be overwritten by or overwrite other structures without your knowledge. Bad situation.

The newer C++11 rvalue references do a somewhat similar thing as in (2.), they allow you to "scavenge" a temporary by reference, omitting a copy. This works due to the fact that the temporary would die immediately after the return statement anyway. Thus, since it's already certain that the temporary is gone, nobody will notice that it's "missing", so another object can "steal" it with not side effects.

#2samoth

Posted 02 August 2012 - 05:44 AM

And what does const really do, i mean what is it really other that making something constant? What does it allow me to do?

The const qualifier does mainly two things:
1. It is a promise (not more than that) to the compiler that you will not modify the value. You can still cast the const away and modify the value anyway, but the compiler will assume that you keep your primise and might make optimizations based this, so that is unwise.
2. It allows you to bind a reference to a temporary, and extends the lifetime of that temporary to the lifetime of the const reference. Otherwise, if you were allowed to bind to a temporary (say, returning a local variable by reference) then whatever you alias does not exist any more by the time you use it, which is bad mojo. You would be reading and possibly modifying some more or less arbitrary data on the stack which might be overwritten by or overwrite other structures without your knowledge. Bad situation.

The newer C++11 rvalue references do a somewhat similar thing as in (2.), they allow you to "scavenge" a temporary by reference, omitting a copy. This works due to the fact that the temporary would die immediately after the return statement anyway. Thus, since it's already certain that the temporary is gone, nobody will notice that it's "missing", so another object can "steal" it with not side effects.

#1samoth

Posted 02 August 2012 - 05:43 AM

And what does const really do, i mean what is it really other that making something constant? What does it allow me to do?

The const qualifier does mainly two things:
1. It is a promise (not more than that) to the compiler that you will not modify the value. You can still cast the const away and modify the value anyway, but the compiler will assume that you keep your primise and might make optimizations based this, so that is unwise.
2. It allows you to bind a reference to a temporary, and extends the lifetime of that temporary to the lifetime of the const reference. Otherwise, if you were allowed to bind to a temporary (say, returning a local variable by reference) then whatever you alias does not exist any more by the time you use it, which is bad mojo.

The newer C++11 rvalue references do a somewhat similar thing as in (2.), they allow you to "scavenge" a temporary by reference, omitting a copy. This works due to the fact that the temporary would die immediately after the return statement anyway. Thus, since it's already certain that the temporary is gone, nobody will notice that it's "missing", so another object can "steal" it with not side effects.

PARTNERS