Skip to content

Ensuring Predictable Destruction Order in cppfront #1112

Closed Answered by hsutter
shemeshg asked this question in Q&A
Discussion options

You must be logged in to vote

Thanks!

without a virtual destructor, the destruction sequence may not be as expected, especially when dealing with inheritance.

Do you have an example that doesn't work as you expect? The reason I ask is because Cpp2 lowers to Cpp1, and so I think if the derived destructor is not marked virtual the Cpp1 semantics make it so.

For example, this code (edit: corrected to make it actually be a pointer to Base):

main: () = {
    pb: std::unique_ptr<Base> = new<Derived>();
}

Base: type = {
    operator=: (virtual move this) = { std::cout << "Base dtor\n"; }
}
Derived: type = {
    this: Base = ();
    operator=: (move this) = { std::cout << "Derived dtor\n"; }
}

prints

Derived dtor
Base dtor

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by shemeshg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants