Comparing exception behavior of magic statics, std::call_once, and std::async
cppdashboard.dev/r/2026/09/comparing-exception-behavior-of-magic-statics-std-call-onceThis article discusses the differences in exception handling between magic statics, std::call_once, and std::async in C++. It explains how each mechanism behaves when an exception occurs during initialization or execution.
From the article
We’ve been comparing magic statics, std:: call_ once , and std:: async , but one thing we haven’t considered is their behavior in the event of an exception.
For magic statics, if an exception occurs during initialization of the static, then the static is considered not to have been initialized. The exception propagates, and the next time the function is called, the language will try to initialize the static again.
For call_ once , if an exception occurs during execution of the lambda, then the call is considered not to have occurred. The exception propagates, so the next time you call call_ once with the same once_ flag , it will try to call it.
Share this resource