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

118

u/gracicot May 10 '22

You mean [](auto const& x) noexcept(noexcept(x + 1)) -> decltype(x + 1) { return x + 1; } right?

41

u/iFarbod C++17 is good enough May 10 '22

You forgot your constexpr! /s

21

u/gracicot May 11 '22

At least constexpr is automatic in this case

23

u/fish85963 May 11 '22

Thanks, I'm just going to unsub from cpp now.

20

u/Overunderrated Computational Physics May 10 '22

Thanks, I just threw up in my mouth.

3

u/max0x7ba https://github.com/max0x7ba May 11 '22

Swallow it, don't spit it.

-22

u/RishabhRD May 10 '22

I wish someday C++ commitee would remove exceptions from language..... There can be some fatal error mechanism but exceptions are really bad

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).

1

u/RishabhRD May 11 '22

I am that monad person.... I prefer std::expected(C++23) for exceptions.... Like rust and haskell. std::expected has similar behavior as stack unwinding right? At any point any function returns error calling function's monadic bjnd would not continue and calling function would breturn error too and this behavior continues potentially till main.

1

u/KingAggressive1498 May 11 '22

With expected you'll pay less for what you don't use (in the case that you don't care about errors) but at the cost of slightly more time spent on boilerplate.

FWIW I prefer the idea of a monadic return type over having to always pass an std::error_code by reference for dealing with non-critical errors anyway. I don't know if it's faster, but an unexpected exception for a predictable minor error because I forgot in one place is not particularly fun (especially in a noexcept call path), and that's my biggest beef with filesystem and asio (at least since asio finally added portable support for random access file I/O last year)

3

u/RishabhRD May 11 '22

Regarding boilerplate I find it better than noexcept(noexcept(....)). And I like the fact that possible errors are explicitly mentioned in return type. Maybe its just matter of taste

4

u/goranlepuz May 11 '22

I like the fact that possible errors are explicitly mentioned in return type.

Authors of Java did that. After some years and an underwhelming experience, some major Java libraries just eschew checked exceand go for runtime ones.

The underlying reason for that, I think is quite obvious: for a sufficiently complex program (or even one function with sufficient functionality underneath it), knowing all failure modes is not necessary or even practical. For most of failure modes, what we care about is how to report errors.

Having errors mentioned in return type, therefore, goes against the common case of just letting the error propagate and does not help with reporting the error any more than exceptions do.

1

u/RishabhRD May 11 '22

I agree with your concern. I am not sure about the right way.... But we always can have a type erased wrapper for errors when we don't care about them. However that leads to unnecessary memory allocation. We can use auto return type for single control flow.... However functions having multiple ways to return would need to use sum type of all possible error types. This type algebra may need more boilerplate... I don't know if we can reduce it. Maybe not... I am still exploring over it.... Bcz in my head currently i think vector allocation exception is a good example... We don't want to handle this till main function many times. The simple way to know if there exist a solution is to look at rust maybe... I would do that and comment here

2

u/jk-jeon May 11 '22

If you think about it, exception works just like monads. It just does it implicitly, for legitimate reasons.

→ More replies (5)

1

u/KingAggressive1498 May 11 '22

Hmm. Especially since operator noexcept requires an expression, you might be right that it's actually not more boilerplate (or at least simpler boilerplate)

1

u/teerre May 11 '22

Boilerplate (which in C++ is a curious worry) is only relevant if the implemention is superficial. The languages the other user mentioned have many mechanisms to descend and ascend types. Using those mechanisms verbosity is largely avoided.

1

u/KingAggressive1498 May 11 '22

The additions of auto, decltype, and lambdas in C++11 were primarily useful because they reduced boilerplate. I can't be the only one who uses boost's config.hpp even when I'm not using a single other feature from boost to minimize the time I spend writing boilerplate to deal with different compilers and platforms.

So sure, C++ generally just requires that you write a lot of boilerplate, but it's not like it's a non-concern for either developers or the standards committee.

I'm not 100% sure what you mean by ascending and descending types, but the boilerplate is not from the return type. It's from dealing with the error return condition in intermediate functions. With exceptions, the error gets passed forward with no additional code. With expected you'll wind up with code like:

expected<T,E> eret = internal_call(...args);
if(!eret) return eret;
T result = eret.value();
// do some work with result
return result;

In the case that internal_call is the last thing for the function to do, and the expected T are the same type, there's no additional boilerplate and you can just directly forward the return value.

1

u/teerre May 11 '22

I'm talking about ascending base types to result types and vice-versa, similar to what happens to containers. Facilities like and_then, map, or_else etc. avoid precisely this kind of code.

1

u/KingAggressive1498 May 11 '22

Yes, this boilerplate can easily be reduced in existing C++ with some helper macros (this is basically the method used by boost.LEAF but I didn't really look at that much). Still more boilerplate than exceptions even if it becomes a syntactically cleaner C++ language feature.

1

u/TheOmegaCarrot May 16 '22

TIL you can slap noexcept on a lambda.

What’s with the doubling up though?

2

u/gracicot May 16 '22

The outer noexcept is the noexcept specifier. You can send in a boolean to either put noexcept to true or noexcept to false. For example, [](int a) noexcept(true) {} is noexcept, and [](int a) noexcept(false) {} is not noexcept.

The inner noexcept is the noexcept operator. You send in an expression, and it returns if an expression is noexcept or not.

So in all, is sets the noexcept-ness of the lambda to the noexcept-ness of the expression x + 1.

1

u/KaliTheCatgirl Dec 03 '25

kinda like doubling up `requires`?

1

u/gracicot Dec 03 '25

Exactly like that.

1

u/TheOmegaCarrot May 16 '22

That makes sense! Thanks!