开发者

Is there any guarantee for the order in which C++ stack variables are destroyed

开发者 https://www.devze.com 2023-01-25 00:01 出处:网络
Consider the following code: { std::auto_ptr<Something> p1(pSomePointer); std::auto_ptr<Something> p2(pSomeOtherPointer);

Consider the following code:

{
  std::auto_ptr<Something> p1(pSomePointer);
  std::auto_ptr<Something> p2(pSomeOtherPointer);
  ...
开发者_StackOverflow中文版}

Is there any guarantee that p2 destructor will be called before p1's when exiting the scope? Common sense says that the stack variables should be destroyed from top of the stack first but C++ compiler can reorder assignments.


Yes, they are destroyed in order opposite to the construction order - objects constructed last will be destroyed first. C++ guarantees this.

0

精彩评论

暂无评论...
验证码 换一张
取 消