You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
EnTT uses a simplified swap-and-pop policy by default (unless you enable something like the in-place delete).
The internals are a little more complicated than what I'm going to describe here but imagine the following for a moment:
You swap the last element with the one to remove. Swapping involves a third (and temporary) element that is discarded immediately after. First destructor invoked.
You pop the last element in the set. Second destructor invoked.
Here we go with your destructors. Two elements, two invocations. That's all.
The fact that you've a single element doesn't really matter. EnTT doesn't place all this after an if that only works in a single case. It would be a waste of cpu cycles for nothing. Instead, the library uses a safe approach that works in all cases.
That being said and for the sake of curiosity, EnTT works in a slightly different manner internally but the final result is similar.
What happens is that the last element is moved into a call to std:.exchange with the element to drop which is returned rather than discarded immediately. Then we pop the last (moved-from) element and you've the first destructor invoked. Finally we let the element to discard go out of scope and you have the second destructor invoked.
Why it's that complicated? Well, all this is meant to support re-entrant destructors. A rather sophisticated feature that most users don't use (and not even know about actually) but which is also very powerful in case you need it.
https://godbolt.org/z/b8MfeaWq5
The text was updated successfully, but these errors were encountered: