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

Why does this: #include <string> #include <iostream> using namespace std; class Sandbox { public: Sandbox(const string& n) : member(n) {} const string& member; }; int main() { Sandbox sandbox(string(“four”)); cout << “The answer is: ” << sandbox.member << endl; return 0; } Give output of: The answer is: Instead of: The answer is: four 6 … Read more

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 X { X& ref() { return *this; } }; X getx() { return X();} … Read more

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 change files’ state to specific commit, run project and, when finished, restore … Read more