Will running a 'equivalent' procedure to an objects destructor result in undefined behavior under the standard?
Example:
Assume we wish to represent a directed acyclic tree, with homogeneous nodes, with root ownership. A problem with deep-structures is recursive operations. Almost all operations can be implemented, of course, with an allocated stack. However, can the nodes remain homogeneous, and automatically destroyed, without undefined behavior?
Namely by bypassing recursive destruction by doing a logically equivalent stack based destruction operation from the root node. (Ignoring possible exceptions from allocation)
class Node {
// ...
// Assuming unique ownership
Node *left;
Node *right;
~Node() { destroy_deep(); }
void destroy()
{
// Assert: left == nullptr && right == nullptr
// ...
}
void destroy_deep()
{
std::vector<Node *> nodes{ this };
while (nodes.size()) {
Node &next = *nodes.back();
if (!next.left && !next.right) {
next.destroy();
if (next != this) {
// Assume such a deallocate function
deallocate(next);
}
nodes.pop_back();
} else {
if (next.left) {
nodes.push_back(next.right);
}
if (next.right) {
nodes.push_back(next.left);
}
}
}
}
};