Andrey Tarasevich
11/19/2008 12:19:00 AM
Jeff Schwab wrote:
> Andrey Tarasevich wrote:
>> Yan wrote:
>>> Can I rely on the order in which destructors for objects 'a' and 'b'
>>> are called in the attached code or it's up to the compiler to decide?
>>>
>>> Thank you.
>>>
>>> -------------------
>>> class A {};
>>> class B {};
>>>
>>> void f() {
>>> A a;
>>> B b;
>>> }
>>> -------------------
>>
>> Yes. In C++ static and automatic objects are destroyed in the reverse
>> order of their creation.
>
> Given that they are in the same translation unit.
No. The global order or destruction is always the reverse of the global
order of construction, whatever the latter might be. It is true that the
order of construction [of static objects] is not specified across
translation units, but once some (any) global construction order took
place during the actual execution of the program, the order of
destruction is required to be the exact reverse of it. Globally, i.e.
across all translation units.
--
Best regards,
Andrey Tarasevich