In GMan’s answer here, the destructor of the restore_base class isn’t virtual, so I keep wondering how exactly that works. Normally you’d expect the destructor of restorer_base to be executed only, after the object goes out of scope, but it seems that the derived restorer_holder destructor is really called. Anyone care to enlighten me?
In GMan’s answer here , the destructor of the restore_base class isn’t virtual ,
Share
The standard case where you need a virtual destructor is
And the standard case where you don’t is
GMan’s code is doing something a little trickier, that turns out to be equivalent to the second case:
objis a bare reference; normally, it would not trigger destructors at all. But it’s initialized from an anonymous temporary object whose static type — known to the compiler — isDerived. When that object‘s lifetime ends, the compiler will call theDeriveddestructor. Normally an anonymous temporary object dies at the end of the expression that created it, but there’s a special case for temporaries initializing a reference: they live till the reference itself dies, which here is the end of the scope. So you get pseudo-scoped_ptrbehavior and you don’t need a virtual destructor.EDIT: Since this has now come up twice: The reference does not have to be
constfor this special rule to apply. C+98 [class.temporary]/5:Emphasis mine. There is no mention of
constin this language, so the reference does not have to beconst.EDIT 2: Other rules in the standard prohibit creation of non-const references to temporary objects that are not lvalues. I suspect that at least some temporary objects are lvalues, but I don’t know for certain. Regardless, that does not affect this rule. It would still be formally true that non-const references to temporary objects prolong their lifetime even if no strictly conforming C++ program could ever create such a reference. This might seem ridiculous, but you’re supposed to read standardese this literally and pedantically. Every word counts, every word that isn’t there counts.