C++ Logo

std-proposals

Advanced search

[std-proposals] Terse lambda

From: Marcin Jaczewski <marcinjaczewski86_at_[hidden]>
Date: Sat, 1 Feb 2025 16:35:20 +0100
One problem with the previous proposal was what value category
should return type be, some prefer it as value other prefer reference.
Question why not allow both? We could have 3 types of arrows:

```
=> //return value (if not specified explicitly)
&=> //return reference
&&=> //return forward reference
```

only `decltype(auto)` is not easily available but this
probably is only for very special cases.

Most safe version need least typing and is simplest.
Another thing syntax is intuitive, when someone sees `&`
one of their first thoughts should be "reference?".

This syntax could be consider of contraction of full syntax:

```
[x]() -> auto& => x;
[x]() -> auto &=> x;
[x]() &=> x;
```

Only drawback of syntax like this could break some current code like:

```
foo<&T::operator=>() // could see `=>` instead of current `=` and `>`
foo<&T::operator&=>() //similar `&=>` as one token.
```

But we could simply handle this similar to `>>` in template context
and require brackets:

```
foo<&T::operator=> //split `=` and `>`
foo<([] => 42)>
```



If someone specify `->` and `=>` like:

```
template<typename T>
void foo(T&& t)
{
    bar([&] -> T& &&=> t);
    bar([&] -> T&& &=> t);
    bar([&] &=> t);
}
```

Then similarly like forward reference parameters, all return types
will have the same value category of `&`.

Received on 2025-02-01 15:35:32