C++ return value created before or after auto var destruction?

痴心易碎 提交于 2019-12-17 06:51:44

问题


In C++ is a return value guaranteed to be created before automatic variables in the function are destroyed? Notice Basket::get:

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 get ()
  {
    Lock lock(mutex);  // Constructor locks, destructor unlocks mutex
    return gift;       // Gift copy constructor
  }
};

I need Basket::get to perform its Gift copy constructor (of the temp object returned) prior to destruction of the lock object. Otherwise the gift object being returned can be corrupted by a simultaneous call to put.

My tests show the gift copy is indeed created before lock destruction, however, is it guaranteed? If not, I'll need to create a second temporary inside the function, such as:

  Gift get ()
  {
    Gift result;
    {
      Lock lock(mutex);
      result = gift;
    }
    return result;
  }

回答1:


Yes, the auto variable will remain in scope until after the return is finished. This is especially true if you are using a compiler that optimizes the return, eg:

Gift get() 
{ 
    Lock lock(mutex);
    return gift;
} 

Gift g = basket.get();

Which would be equivilent to this sequence:

Gift g;
Lock lock(mutex);
g = Gift(gift);
~lock();

May be optimized to act more like this:

void get(Gift &ret) 
{ 
    Lock lock(mutex);
    ret = gift;
} 

Gift g;
basket.get(g);

Which would be equivilent to this sequence:

Gift g;
Lock lock(mutex);
g = gift;
~lock();

In other words, a temporary can be removed during the return.




回答2:


It's guaranteed. The return value is copied (if necessary) before the destruction occurs. Here's a similar question / answer that gives a good description of the sequence.

Scope and return values in C++



来源:https://stackoverflow.com/questions/8437763/c-return-value-created-before-or-after-auto-var-destruction

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!