在C++中是否保证在函数中的自动变量被销毁之前创建的返回值?公告篮::获取:
class Basket
{
public:
// Gift is a struct containing safely copyable things like int or string
Gift gift;
// Used to protect access and changes to gift
Mutex mutex;
// Copy gift into present, while locked to be thread safe
void put (const Gift & gift)
{
Lock lock(mutex); // Constructor locks, destructor unlocks mutex
this->gift = gift; // Gift assignment operator
}
// Return a memberwise-copy of gift, tries to be thread safe (but is it?)
Gift …Run Code Online (Sandbox Code Playgroud)