r/cpp • u/pavel_v • Apr 25 '26
Defending against exceptions in a scope_exit RAII type
https://devblogs.microsoft.com/oldnewthing/20260424-00/?p=112266
36
Upvotes
r/cpp • u/pavel_v • Apr 25 '26
26
u/Umphed Apr 25 '26
So... You don't defend against it, you just say its UB and move on. Titles kinda click bait.
4
u/fdwr fdwr@github 🔍 Apr 25 '26
Yeah, I was wondering how exception locations 1 and 3 are even a problem in practice, because you'd typically want to operate on the actual local variables (not a copy of them), and the assignment should be in-place/RVO. Alas, although it's an improvement for those one-off cleanup cases, a lambda still isn't quite as efficient stackwise nor as concise as just saying C2Y's
defer action;.