Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat are С++ temporaries?
    primarykey
    data
    text
    <p>I was reading the constant chapter in Eckel and got mixed up in the part where Temporaries were explained . What I could get was that when we pass the reference to a function , the compiler creates a temporary which is a const object and so we can't modify it even if we pass a reference as</p> <pre><code>f(int &amp;a){} </code></pre> <p>Now I tried to look at some other references for Temporaries online and got stuck up<br><br> <a href="http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8l.doc%2Flanguage%2Fref%2Fcplr382.htm" rel="nofollow noreferrer">http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8l.doc%2Flanguage%2Fref%2Fcplr382.htm</a> and<br><br> <a href="https://stackoverflow.com/questions/2145030/are-all-temporaries-rvalues-in-c">Are all temporaries rvalues in C++?</a><br><br> which prompted me that temporaries are much more than just passing references and creating const object for that inside the functions.Now I could get something out of these two links but could not say that I have understood the working , functionality and use of temporaries as a whole. It would be really helpful if someone could explain the concept of temporaries. Thanks in advance.<br>The original example of bruce eckel is:<br></p> <pre><code>// Result cannot be used as an lvalue class X { int i; public: X(int ii = 0); void modify(); }; X::X(int ii) { i = ii; } void X::modify() { i++; } X f5() { return X(); } const X f6() { return X(); } void f7(X&amp; x) { // Pass by non-const reference x.modify(); } int main() { f5() = X(1); // OK -- non-const return value f5().modify(); // OK // Causes compile-time errors: //! f7(f5()); //! f6() = X(1); //! f6().modify(); //! f7(f6()); } ///:~ </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload