C++ std::tuple order of destruction

I’ll offer a life lesson I’ve learned, rather than a direct answer, in response to your question:

If you can formulate, for multiple alternatives, a reasonable argument for why that alternative should be the one mandated by the standard – then you should not assume any of them is mandated (even if one of them happens to be).

In the context of tuples – please, please be kind to the people maintaining your code and do not allow the destruction order of tuple elements to potentially mess up the destruction of other elements. That’s just evil… imagine the hapless programmer who will need to debug this thing. In fact, that poor soul might be yourself in a few years, when you’ve already forgotten about your clever trick from back-in-the-day.

If you absolutely must rely on destruction order, perhaps you should just be using a proper class with the tuple’s elements as its data members (which you could write a destructor for, making it clear what needs to happen in what order), or some other arrangement facilitating a more explicit control of the destruction.

Leave a Comment