jbcoe / jbcoe/mutex_protected

Should `with` return a value from the lambda?

Open
#44 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
CMake
Stars
2
Forks
0
PR merge metrics
No merged PRs in 30d

Description

https://news.ycombinator.com/item?id=35464152#35464934

```cpp
auto x = state.with([](auto& state) { return state.x; });
```

If you're mutating the protected state
```cpp
state.with([](auto& v) { v++; });
```
the current `void with(F&&)` is fine, but if you're reading the state,
```cpp
count += state.with([](auto& v) { return v; });
```
is certainly nicer than
```cpp
state.with([&count](auto& v) { count += v; });
```
though I guess it needs an extra copy.

Considerations:
- Can we enforce that the return value is copied out instead of returned by reference? Should we?
- What about the try variants? I guess then we have to return a std::optional to show whether there is a return value at all?

If we decide against this, can/should we enforce that the lambda return type is void?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.