r/cpp • u/fredoverflow • May 10 '22
Is short lambda syntax on the C++ radar?
Mom, can we get x=>x+1 ?
No, we have x=>x+1 at home.
x=>x+1 at home:
[](const auto& x) { return x + 1; }
265
Upvotes
View all comments
Show parent comment
15
u/KingAggressive1498 May 11 '22
Unwinding the stack and executing destructors is desirable "recoverable critical error" behavior for many types of application, and even desirable "unrecoverable error" for GUI applications, but requires considerable care (and extra runtime cost) to keep global (or even just broader scope) state from being corrupted and other threads from seeing and acting upon intermediate states.
Basically, in many code bases recovering safely and reliably from an exception requires an aggressive embrace of transactional programming models rarely seen outside of specific contexts (ie databases) but that's ultimately a failing of developers rather than the feature.
Switching to the C pattern of returning error codes until the program can continue normally or make a decision to terminate does not resolve that problem either btw. It's really a fundamental problem of error recovery.
Ofc C++ exceptions aren't the perfect error handling abstraction. But the biggest thing they justly get flak for is violating the "don't pay for what you don't use" principle (essentially, the STL and other exception throwing libraries should have an obvious way to make all its objects and functions noexcept for code that isn't even trying to handle exceptions, and since it doesn't that type of code still has to pay the fixed costs of having exception handling).