temporaries

C++ Copy constructor, temporaries and copy semantics

For this program #include <iostream> 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 = f(); C b = a; return 0; } the output I get is: Entered f()! Default C called! ...

When should I use temporary variables?

Specifically, I'm wondering which of these I should write: { shared_ptr<GuiContextMenu> subMenu = items[j].subMenu.lock(); if (subMenu) subMenu->setVisible(false); } or: { if (items[j].subMenu.lock()) items[j].subMenu.lock()->setVisible(false); } I am not required to follow any style guidelines. After o...

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 fields, and while the actual value of the generator isn't important, it is important that th...