Questions tagged [temporary-objects]

C++ Temporary objects are unnamed objects created on the stack by the compiler. They are used during reference initialization and during evaluation of expressions including standard type conversions, argument passing, function returns, and evaluation of the throw expression.

C++ Temporary objects are unnamed objects created on the stack by the compiler. They are used during reference initialization and during evaluation of expressions including standard type conversions, argument passing, function returns, and evaluation of the throw expression.

When a temporary object is created to initialize a reference variable, the name of the temporary object has the same scope as that of the reference variable. When a temporary object is created during the evaluation of a full-expression (an expression that is not a subexpression of another expression), it is destroyed as the last step in its evaluation that lexically contains the point where it was created.

Reference: http://scv.bu.edu/computation/bluegene/IBMdocs/compiler/xlc-8.0/html/language/ref/cplr382.htm

Answer: Temporary objects - when are they created, how do you recognise them in code?

282 questions
77
votes
2 answers

Cv-qualifications of prvalues (revisited)

This is a followup to my previous question, where the apparent consensus was that the change in treatment of cv-qualifications of prvalues was just a fairly minor and inconsequential change intended to solve some inconsistencies (e.g. functions…
AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
68
votes
4 answers

Why not non-const reference to temporary objects?

Possible Duplicate: Does a const reference prolong the life of a temporary? prolonging the lifetime of temporaries C++ allows assignment of temporary objects only to const reference. It wont allow assignement of temporary objects to…
user738471
  • 879
  • 1
  • 8
  • 10
48
votes
4 answers

Why doesn't a const reference extend the life of a temporary object passed via a function?

In the following simple example, why can't ref2 be bound to the result of min(x,y+1)? #include template< typename T > const T& min(const T& a, const T& b){ return a < b ? a : b ; } int main(){ int x = 10, y = 2; const int& ref…
42
votes
4 answers

Temporary objects - when are they created, how do you recognise them in code?

In Eckel, Vol 1, pg:367 //: C08:ConstReturnValues.cpp // Constant return by value // 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()…
user621819
36
votes
1 answer

How can the type of braces influence object lifetime in C++?

A friend of mine showed me a program in C++20: #include struct A { A() {std::cout << "A()\n";} ~A() {std::cout << "~A()\n";} }; struct B { const A &a; }; int main() { B x({}); std::cout << "---\n"; B y{{}}; …
Fedor
  • 17,146
  • 13
  • 40
  • 131
29
votes
3 answers

Are the addresses of two temporaries guaranteed to be different in the same expression?

Consider the following program: #include int const * f(int const &i) { return &i; } int main() { std::cout << f(42); // #1 std::cout << f(42); // #2 std::cout << f(42) << f(42); // #3 } Depending on the compiler, and…
cigien
  • 57,834
  • 11
  • 73
  • 112
26
votes
2 answers

Non-const reference bound to temporary, Visual Studio bug?

I ran into this while compiling some portable code in gcc. Basically this strange code compiles in Visual studio which really just blows my mind: class Zebra {int x;}; Zebra goo() {Zebra z; return z;} void foo(Zebra &x) { Zebra y; x = y; …
user805547
  • 1,245
  • 1
  • 15
  • 23
24
votes
7 answers

Performance of pIter != cont.end() in for loop

I was getting through "Exceptional C++" by Herb Sutter lately, and I have serious doubts about a particular recommendation he gives in Item 6 - Temporary Objects. He offers to find unnecessary temporary objects in the following code: string…
Mikhail
  • 20,685
  • 7
  • 70
  • 146
24
votes
2 answers

const reference to temporary vs. return value optimization

I'm aware of the fact that assigning an rvalue to a const lvalue reference extends the temporaries lifetime until the end of the scope. However, it is not clear to me when to use this and when to rely on the return value optimization. LargeObject…
23
votes
1 answer

Is taking a reference from a temporary valid C++ code?

I used the following syntactic sugar: for (auto& numberString: {"one", "two", "three", "four"}) { /* ... */} Is this valid code? AFAIK, based on this question, this should be illegal, yet the code runs as expected. I don't think my understanding is…
Dávid Tóth
  • 2,788
  • 1
  • 21
  • 46
21
votes
1 answer

Is constructing an object in an argument list and passing a pointer to internal data of the object to the function safe?

Is the C++ code below well-formed? Will the std::string get destroyed before or after the function finishes executing? void my_function(const char*); ... my_function(std::string("Something").c_str()); I know I could do my_function("Something"),…
BlueCannonBall
  • 365
  • 2
  • 5
20
votes
4 answers

Where are temporary object stored?

Is it true that temporary objects are stored in dynamic (heap) memory?
Mihran Hovsepyan
  • 10,810
  • 14
  • 61
  • 111
20
votes
2 answers

Lifetime extension and the conditional operator

local lvalue references-to-const and rvalue references can extend the lifetime of temporaries: const std::string& a = std::string("hello"); std::string&& b = std::string("world"); Does that also work when the initializer is not a simple expression,…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
19
votes
2 answers

Am I right in saying that const_cast followed by modification on a ref-to-const bound to a temporary is okay?

I would like to check my understanding and conclusions on this matter. On IRC, it was asked: Is it acceptable to const_cast a const reference that's bound to a temporary object? Translating: he has a ref-to-const bound to a temporary, and he…
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
19
votes
2 answers

Temporary lifetime extension

Section 12.2.5 of the standard says: A temporary bound to a reference parameter in a function call (5.2.2) persists until the completion of the full expression containing the call. A temporary bound to the returned value in a function…
Kolyunya
  • 5,973
  • 7
  • 46
  • 81
1
2 3
18 19