Questions tagged [temporaries]
25 questions
34
votes
7 answers
Are all temporaries rvalues in C++?
I have been coding in C++ for past few years. But there is one question that I have not been able to figure out. I want to ask, are all temporaries in C++, rvalues?
If no, can anyone provide me an example where temporary produced in the code is an…

Prasoon Saurav
- 91,295
- 49
- 239
- 345
16
votes
3 answers
C++ Copy constructor, temporaries and copy semantics
For this program
#include
using std::cout;
struct C
{
C() { cout << "Default C called!\n"; }
C(const C &rhs) { cout << "CC called!\n"; }
};
const C f()
{
cout << "Entered f()!\n";
return C();
}
int main()
{
C a =…

legends2k
- 31,634
- 25
- 118
- 222
12
votes
1 answer
Binding temporary to const reference in c'tor initializer list
Section 12.2.5 in C++03 says "A temporary bound to a reference member in a
constructor’s ctor-initializer (12.6.2) persists until the constructor exits"
So I tried following program
#include
using namespace std;
struct foo
{
foo()
…

Happy Mittal
- 3,667
- 12
- 44
- 60
11
votes
3 answers
Why is there no gcc/g++ warning for unused temporaries?
Consider the following code :
void ListenerImpl::attach(boost::shared_ptr subscriber)
{
boost::unique_lock(mtx);
subscribers.push_back(subscriber);
}
void ListenerImpl::notify(MsgPtr msg)
{
…

Gabor Marton
- 2,039
- 2
- 22
- 33
11
votes
2 answers
If temporaries are implicitly non-modifiable, how does this work?
I'm told that, in C++03, temporaries are implicitly non-modifiable.
However, the following compiles for me on GCC 4.3.4 (in C++03 mode):
cout << static_cast(stringstream() << 3).str();
How is this compiling?
(I am not talking about…

Lightness Races in Orbit
- 378,754
- 76
- 643
- 1,055
8
votes
2 answers
Prevent temporary from extending its lifetime?
This may be impossible, but I was wondering if it was possible to keep a temporary from ever lasting past its original expression. I have a chain of objects which point to parent objects, and a member function which will create a child object, a…

zounds
- 773
- 8
- 17
7
votes
2 answers
BOOST_FOREACH Iteration over boost::shared_ptr
I'm doing something similar to this item Correct BOOST_FOREACH usage?
However, my returned list is wrapped in a boost::shared_ptr. If I do not assign the list to a variable before the BOOST_FOREACH loop, I get a crash at runtime as the list is…

PeskyGnat
- 2,454
- 19
- 22
5
votes
2 answers
Can temporaries bind to non-const references?
I wrote the following code to test this:
struct X
{
char* x;
X()
{
x = new char('a');
}
~X()
{
*x = 'b';
delete x;
}
};
void foo(const X& x)
{
}
void goo(X& x)
{
}
int main()
{
foo(X());
…

AMCoder
- 773
- 1
- 6
- 15
4
votes
3 answers
What exactly are temporaries in C++ and in which specific cases the compiler will create them?
I am a C++ newbie and I am struggling on the temporaries topic. I don't find anywhere a clear list of all cases in which the compiler will create a temporary. Actually, a few days ago I had in mind that when we pass an rvalue as const reference…

Francesco Ferrai
- 63
- 4
4
votes
2 answers
Show where temporaries are created in C++
What is the fastest way to uncover where temporaries are created in my C++ code?
The answer is not always easily deducible from the standard and compiler optimizations can further eliminate temporaries.
I have experimented with godbolt.org and its…

Patrick Fromberg
- 1,313
- 11
- 37
4
votes
3 answers
How does the compiler determine the needed stack size for a function with compiler generated temporaries?
Consider following code:
class cFoo {
private:
int m1;
char m2;
public:
int doSomething1();
int doSomething2();
int doSomething3();
}
class cBar {
private:
cFoo mFoo;
public:
…

Matthias
- 43
- 7
3
votes
5 answers
Working around the C++ limitation on non-const references to temporaries
I've got a C++ data-structure that is a required "scratchpad" for other computations. It's not long-lived, and it's not frequently used so not performance critical. However, it includes a random number generator amongst other updatable tracking…

Eamon Nerbonne
- 47,023
- 20
- 101
- 166
2
votes
0 answers
Binding temporaries to references
Possible Duplicate:
How come a non-const reference cannot bind to a temporary object?
First up, I understand that the standard mandates that temporaries can only be bound to const references, and, as I understand it, in this case the lifetime of…

Darren Engwirda
- 6,915
- 4
- 26
- 42
2
votes
10 answers
When should I use temporary variables?
Specifically, I'm wondering which of these I should write:
{
shared_ptr subMenu = items[j].subMenu.lock();
if (subMenu)
subMenu->setVisible(false);
}
or:
{
if (items[j].subMenu.lock())
…

Kyle
- 4,487
- 3
- 29
- 45
1
vote
4 answers
temporaries not behaving as const
Its unclear to me whether a temporary assumes type of const or not, in an expression as shown below.
#include
class X {
public:
X(int a) { i = a; cout << "X(int) [" << (int)this << "]" << endl; }
X& operator+(const X& x)
{
i += x.i;…

Chethan
- 905
- 1
- 10
- 18