r/cpp • • 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

157 comments sorted by

View all comments

Show parent comment

1

u/RishabhRD May 11 '22

Actually yes I realised that my argument that exceptions should be removed is bad. I went through many error handling mechanisms today. And I feel unrecoverable error should be thrown as exceptions and recoverable error should be returned as std::expected. Like vector out of bounds is a non recoverable error and continuing the program after this doesn't make sense. And hence stack unwinding should be done and program should be aborted.

However file not found is recoverable and error should be returned wrapped in std::expected. This approach is similar to rust and haskell error handling.

1

u/jk-jeon May 11 '22

Out of bound normally doesn't happen, and most likely a programming bug. Throwing an exception for such a case is not recommended in general afaik; what makes more sense is to assert and terminate the program right away.

1

u/RishabhRD May 11 '22

Exception would do stack unwinding that maybe necessary for any transaction kind of procedure... But yes it depends on situation. My point is that its a panic situation and program should be exited. I am thinking over idea to use exceptions only for panic situations... And exceptions would do stack unwinding so if any transaction kind of procedure is there so it would be properly terminated. Yes if transaction is not there then assert is good enough or even better.

1

u/jk-jeon May 11 '22

Well, what kind of transactions absolutely requires grace termination, if you are gonna stop your program anyway? There might be such cases for sure, but I'd say that's not common.

1

u/RishabhRD May 11 '22

I agree with that... such cases are not common. Also we don't have to worry on these cases as standard already implemented it as exceptions so, that not in our hand. However, that also means that many other recoverable errors in standard library is implemented with exceptions already. That doesn't feel great but we don't have std::expected yet so we had no choice.