temporary

Temporarily switch working copy to a specific Git commit

烂漫一生 提交于 2019-11-30 10:03:10
问题 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 change files' state to specific commit, run project and, when finished, restore files back to last commit. How to do this without zipping the whole project's folder? 回答1: If you are at a certain branch mybranch , just go ahead and git checkout

Full-expression boundaries and lifetime of temporaries [duplicate]

时光怂恿深爱的人放手 提交于 2019-11-30 08:27:12
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: C++: Life span of temporary arguments? It is said that temporary variables are destroyed as the last step in evaluating the full-expression, e.g. bar( foo().c_str() ); temporary pointer lives until bar returns, but what for the baz( bar( foo().c_str() ) ); is it still lives until bar returns, or baz return means full-expression end here, compilers I checked destruct objects after baz returns, but can I rely on

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

徘徊边缘 提交于 2019-11-30 04:24:54
Is it possible to return a standard container from a function without making a copy? Example code: std::vector<A> MyFunc(); ... std::vector<A> b = MyFunc(); As far as I understand, this copies the return value into a new vector b. Does making the function return references or something like that allow avoiding the copy? Steve Townsend If your compiler supports the NRVO then no copy will be made, provided certain conditions are met in the function returning the object. Thankfully, this was finally added in Visual C++ 2005 (v8.0) This can have a major +ve impact on perf if the container is large

Address of a temporary in Go?

独自空忆成欢 提交于 2019-11-30 01:17:37
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 the heap if its address is taken. Here it's clear that the address of the return value is to be taken. What's an idiomatic way to handle this? zzzz The address operator returns a pointer to something having a "home", e.g. a variable. The value of the expression in your code is "homeless". if you really need a *string, you'll have to do it in 2 steps: tmp :=

Temporarily switch working copy to a specific Git commit

 ̄綄美尐妖づ 提交于 2019-11-29 19:02:22
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 change files' state to specific commit, run project and, when finished, restore files back to last commit. How to do this without zipping the whole project's folder? Alexander Pavlov If you are at a certain branch mybranch , just go ahead and git checkout commit_hash . Then you can return to your branch by git checkout mybranch . I had the same game

Printing a string to a temporary stream object in C++

天涯浪子 提交于 2019-11-29 15:03:01
I have a special type of ostringstream that I am trying to output text to as a temporary object but I'm having some trouble. To be clear, this is essentially what I want to do: ostringstream() << "PARTY DOWN!" << endl; Now before you say: "But Zack, that code is totally worthless! The object is destroyed at the end of the line, how would you even know if it did anything?", hear me out. I don't try to do this with plain ostringstreams, but rather a derived class in which the destructor actually provides a path for the data to exit the object. So in reality, it looks a lot more like this:

How to pass a temporary array?

▼魔方 西西 提交于 2019-11-29 14:10:00
How can I pass a temporary array? I want to do something like this: #include <iostream> int sum(int arr[]) { int answer = 0; for (const auto& i : arr) { answer += i; } return answer; } int main() { std::cout << sum( {4, 2} ) << std::endl; // error std::cout << sum( int[]{4, 2} ) << std::endl; // error } Do I need a positive integer literal in the function parameter's braces [] ? If I include that literal, will it limit what arrays I can pass to only arrays of that size? Also, how can I pass array elements by rvalue reference or const reference? Because the above sample doesn't compile, I

Construction of temporary in function call is interpreted as declaration

你。 提交于 2019-11-29 13:36:19
Lately I ran into a problem which somehow (but only somehow) makes sense to me. It is based on interpreting the construction of a temporary as declaration of the single (!) constructor argument. Please have a look at the minimal example below. #include <iostream> class Foo0{ public: Foo0(int a){}; void doStuff() {std::cout<<"maap"<<std::endl;}; }; class Foo1{ public: Foo1(int a){}; void doStuff() {std::cout<<"maap"<<std::endl;}; }; class Foo2{ public: Foo2(int a){}; void doStuff() {std::cout<<"maap"<<std::endl;}; }; class Bar{ public: Bar(Foo0 foo0, Foo1 foo1, Foo2 foo2){}; }; int main () {

rvalues and temporary objects in the FCD

与世无争的帅哥 提交于 2019-11-29 11:03:52
It took me quite some time to understand the difference between an rvalue and a temporary object. But now the final committee draft states on page 75: An rvalue [...] is an xvalue, a temporary object or subobject thereof, or a value that is not associated with an object. I can't believe my eyes. This must be an error, right? To clarify, here is how I understand the terms: #include <string> void foo(std::string&& str) { std::cout << str << std::endl; } int main() { foo(std::string("hello")); } In this program, there are two expressions that denote the same temporary object : the prvalue std:

Pass temporary object to function that takes pointer

偶尔善良 提交于 2019-11-29 10:47:58
I tried following code : #include<iostream> #include<string> using namespace std; string f1(string s) { return s="f1 called"; } void f2(string *s) { cout<<*s<<endl; } int main() { string str; f2(&f1(str)); } But this code doesn't compile. What I think is : f1 returns by value so it creates temporary, of which I am taking address and passing to f2. Now Please explain me where I am thinking wrong? The unary & takes an lvalue (or a function name). Function f1() doesn't return an lvalue, it returns an rvalue (for a function that returns something, unless it returns a reference, its return value is