Questions tagged [temporary]

The concept of any object being ephemeral, for example temporary files or object that will only exist for a short period of time.

The concept of any object being ephemeral, for example temporary files or object that will only exist for a short period of time.

Use this tag if you are asking about concepts and libraries that have a focus on ephemeral objects. Additionally, you should have reason to believe your problem is in some way related to the ephemeral objects.

539 questions
321
votes
3 answers

Temporarily switch working copy to a specific Git commit

How to switch to specific Git commit without losing all the commits made after it? I want that local files will be changed, but commits' database will remain intact, only the current position pointer is set to currently selected commit. I want to…
Paul
  • 25,812
  • 38
  • 124
  • 247
253
votes
11 answers

How come a non-const reference cannot bind to a temporary object?

Why is it not allowed to get non-const reference to a temporary object, which function getx() returns? Clearly, this is prohibited by C++ Standard but I am interested in the purpose of such restriction, not a reference to the standard. struct…
Alexey Malistov
  • 26,407
  • 13
  • 68
  • 88
198
votes
6 answers

Does a const reference class member prolong the life of a temporary?

Why does this: #include #include using namespace std; class Sandbox { public: Sandbox(const string& n) : member(n) {} const string& member; }; int main() { Sandbox sandbox(string("four")); cout << "The answer…
Kyle
  • 4,487
  • 3
  • 29
  • 45
89
votes
6 answers

Why do I need std::get_temporary_buffer?

For what purpose I should use std::get_temporary_buffer? Standard says the following: Obtains a pointer to storage sufficient to store up to n adjacent T objects. I thought that the buffer will be allocated on the stack, but that is not true.…
Kirill V. Lyadvinsky
  • 97,037
  • 24
  • 136
  • 212
75
votes
5 answers

Do temp variables slow down my program?

Suppose I have the following C code: int i = 5; int j = 10; int result = i + j; If I'm looping over this many times, would it be faster to use int result = 5 + 10? I often create temporary variables to make my code more readable, for example, if…
Elliot Gorokhovsky
  • 3,610
  • 2
  • 31
  • 56
74
votes
3 answers

Using index, using temporary, using filesort - how to fix this?

I'm working on a event tracking system which uses a handful of lookup tables as well as the primary logging table. In a report I'm writing, an object can be selected to view statistics against. The interface shows all objects in order of…
a coder
  • 7,530
  • 20
  • 84
  • 131
59
votes
9 answers

Address of a temporary in Go?

What's the cleanest way to handle a case such as this: func a() string { /* doesn't matter */ } b *string = &a() This generates the error: cannot take the address of a() My understanding is that Go automatically promotes a local variable to…
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
53
votes
4 answers

MySQL: Auto increment temporary column in select statement

How do I create and auto increment a temporary column in my select statement with MySQL? Here is what I have so far: SET @cnt = 0; SELECT (@cnt =@cnt + 1) AS rowNumber, rowID FROM myTable WHERE CategoryID = 1 Which returns:…
Sg1456
  • 954
  • 2
  • 10
  • 17
37
votes
2 answers

C++ - using const reference to prolong a member of a temporary, ok or UB?

consider something like this: #include struct C { C(double x=0, double y=0): x(x) , y(y) { std::cout << "C ctor " << x << " " <
user2717954
  • 1,822
  • 2
  • 17
  • 28
37
votes
2 answers

Lifetime of temporaries

The following code works fine, but why is this correct code? Why is the "c_str()" pointer of the temporary returned by foo() valid? I thought, that this temporary is already destroyed when bar() is entered - but it doesn't seem to be like this. So,…
Frunsi
  • 7,099
  • 5
  • 36
  • 42
36
votes
7 answers

Why lifetime of temporary doesn't extend till lifetime of enclosing object?

I know that a temporary cannot be bound to a non-const reference, but it can be bound to const reference. That is, A & x = A(); //error const A & y = A(); //ok I also know that in the second case (above), the lifetime of the temporary created out…
Nawaz
  • 353,942
  • 115
  • 666
  • 851
35
votes
3 answers

Why is it legal to borrow a temporary?

Coming from C++, I'm rather surprised that this code is valid in Rust: let x = &mut String::new(); x.push_str("Hello!"); In C++, you can't take the address of a temporary, and a temporary won't outlive the expression it appears in. How long does…
Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
35
votes
3 answers

Returning a c++ std::vector without a copy?

Is it possible to return a standard container from a function without making a copy? Example code: std::vector MyFunc(); ... std::vector b = MyFunc(); As far as I understand, this copies the return value into a new vector b. Does making the…
static_rtti
  • 53,760
  • 47
  • 136
  • 192
30
votes
3 answers

Creating temporary view from a temporary table in SQL Server

I have a temporary table and I would like to create a temporary view over this temporary table. Is it possible? In following example I would like #Top10Records to be a view instead of a table so that I get select * into #Top10Records from (select…
Thunder
  • 10,366
  • 25
  • 84
  • 114
29
votes
8 answers

Disallowing creation of the temporary objects

While debugging crash in a multithreaded application I finally located the problem in this statement: CSingleLock(&m_criticalSection, TRUE); Notice that it is creating an unnamed object of CSingleLock class and hence the critical section object…
Naveen
  • 74,600
  • 47
  • 176
  • 233
1
2 3
35 36