3 ms·
Calling arbitrary callbacks from a destructor is a bad idea. Sooner or later someone will violate the requirement about exceptions, and your program will be ter
by meindnoch 10mo ago
Calling arbitrary callbacks from a destructor is a bad idea. Sooner or later someone will violate the requirement about exceptions, and your program will be terminated immediately. So I'd only use this pattern in -fno-exceptions projects.
In a similar vein, care must be taken when calling arbitrary callbacks while iterating a data structure - because the callback may well change the data structure being iterated (classic example is a one-shot event handler that unsubscribes when called), which will break naïvely written code.
- fjjdrnrbbrb 10mo agoThrowing exceptions in destructors in C++ is fine, as long as your code is exception safe. What exactly are you referring to?
- meindnoch 10mo agoThrowing exceptions from destructors is "fine", except when the destructor is executed by stack unwinding (triggered by an earlier exception), in which case throwing will terminate the program with std::terminate.