temporaries:
Definition: a temporary is a C++ object which is niether a constant nor a variable, but is has type.

example1:

TYPE f(int a)
{...}

f(2) is a temporary. it is niether a constant nor a variable but it has a type.
example2:

class A
{
public:
      A(char ch)
      {...}
};

(A)'a' is a temporary.

properties of temporaries:
first essential property of temporaries: a temporary is destroyed as soon as possible:
example:

#include <conio.h> 
#include <iostream> 
using namespace std; 
 
class Test 
{ 
public: 
      int a; 
      Test(int x) 
      { 
            a = x; 
            cout<< "object constructed" << "\n"; 
      } 
      Test(const Test& t) 
      { 
            a = t.a; 
            cout<< "object copied" << "\n"; 
      } 
      ~Test() 
      { 
            a = 0; 
            cout<< "object destroyed" << "\n"; 
      } 
}; 
int main() 
{ 
      cout<< ((Test)10).a << "\n"; //1: object constructed, 2: 10, 3: object destroyed 
      cout<< "end"; 
      _getch(); 
}

Output:

object constructed
10
object destroyed
end

(Test)10 is a temporay. it is trying to find an opertunity to be destoyed. we see that it is destroyed before cout<< "end" .

second essential property of temporaries: a temporary tries not to call copy constaructor if possible:
example:

#include <conio.h> 
#include <iostream> 
using namespace std; 
 
class A 
{ 
public: 
      int a; 
      A(int x) 
      { 
            cout<< "constructor" "\n"; 
            a = x; 
      } 
      A(const A& a) 
      { 
            cout<< "copy constructor" "\n"; 
            this -> a = a.a; 
      } 
}; 
void f(A a) 
{ 
      cout<< a.a << "\n"; 
} 
A g() 
{ 
      return A(20); 
} 
int main() 
{ 
      f(g()); 
      _getch(); 
}

Output:

constructor
20

in this example copy constructor must be call at least 2 times. but it is not called at all because A(20) and g() are temporaries.

third essential property of temporaries: in most compilers (inluding Dev C++) a reference to a temporary must be constant, & such reference prevents the temporary to be destroyed until the end of the block execution.
example:

#include <conio.h> 
#include <iostream> 
using namespace std; 
 
class A 
{ 
public: 
      int a; 
      A(int x) 
      { 
            a = x; 
      } 
      ~A() 
      { 
            cout<< "destructor" "\n"; 
      } 
}; 
A f() 
{ 
      A a(10); 
      return a; 
}                              // 1: destrcutor 
int main() 
{ 
      { 
            const A& a = f(); 
            cout<< "end" "\n"; // 2: end 
      }                        // 3: destructor 
      _getch(); 
}

output:

destructor
end
destructor

if you write: A& a = f(); instead of const A& a = f(); VC++ has no problem with it but Dev C++ refuses to compile.

now "temporary" is not a standard name, I defined it myself. I wanted to know if there's a standard name & if there' more info somewhere on the web?
the books about C++ say nothing about temporaries. experts in other forums said that there are conditions under which copy constructor is not called but they didn't say anything about temporaries & its main name if any..

Dani AI

Generated

raised the right question: the informal word "temporary" is indeed the everyday name, while the standard formalizes the concept and (since C++11/17) uses value-category terms such as prvalue, xvalue and glvalue. These categories describe whether an expression names an object or yields a short‑lived value. (cppreference.net)

The basic lifetime rule is simple but the exceptions matter: temporaries are destroyed at the end of the full‑expression that created them, but binding a temporary to a reference can extend its lifetime in many cases — with important exceptions (for example, a temporary bound to a reference member in a constructor’s mem‑initializer does not live for the enclosing object’s lifetime). Read the reference‑initialization and temporary‑object sections for the exact cases. (en.cppreference.com)

Copy elision and move semantics explain why you often see no copies: compilers elide copies (RVO/NRVO) and C++17 made many elisions mandatory by changing prvalue semantics so objects are materialized directly into their final storage. Since C++11 you can also bind temporaries to rvalue references (T&&) to implement move semantics instead of copying. If code compiled on an old MSVC but not on another compiler, that was likely a vendor extension (don’t rely on it). (en.cppreference.com)

Practical tips: prefer returning by value (modern compilers elide/move efficiently), take parameters as const T& for read‑only access or T&&/by‑value for movable ownership, and avoid binding non‑const lvalue references to temporaries (it’s non‑portable). was right to point to the standard — use cppreference or the standard wording for the exact corner cases when debugging lifetime issues. (en.cppreference.com)

// example: bind temporary to rvalue reference (C++11+)
struct S { S(); S(S&&); };
S makeS();
void sink(S&&); 
sink(makeS()); // binds temporary to S&&, moves into sink

Recommended Answers

All 4 Replies

Grab a draft of the C++ standard (google is your friend) and search for "Temporary objects".

And can anyone add any more essential property? those were I founded by experience!

C++ standard

oooh, I have a C# standard & its harder than Shakespears texts to read! |-:

>And can anyone add any more essential property?
No, nobody can add more than the standard itself without entering the realm of implementation-defined and undefined behavior. If you don't understand the standard, specify which parts and I'll help you interpret it. But that particular section (12.6, IIRC) is very lucid, in my opinion.

>I have a C# standard & its harder than Shakespears texts to read! |-:
Standards are like that. You get used to it.

No, nobody can add more than the standard itself without entering the realm of implementation-defined and undefined behavior. If you don't understand the standard, specify which parts and I'll help you interpret it. But that particular section (12.6, IIRC) is very lucid, in my opinion.

no , don't bother. Only wanted to know if I missed knowing another important thing!
thanks for yr answer,
remember you said u wr nt gna ansr! ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.