boostorg / boostorg/optional

[Idea] boost::optional::map for void f(...)

Open
#140 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
60
Forks
75
PR merge metrics
No merged PRs in 30d

Description

As I work boost::optional I have a usecase, where I retrieve optional value and I do not necessary want to transform it, but just to use it if it is there, and if not to ignore it.
Let's assume scenario:
```
boost::optional GetTextOpt();
int stoi(std::string_view);
void SleepMillis(int);
```

In current implementation, workarounds are necessary, like:
```
// v1
if(auto res = GetTextOpt().map(stoi)) SleepMillis(res);
// v2
GetTextOpt().map(stoi).map([](int i){SleepMillis(i); return int{};})
```

Proposal:
Provide new method (e.g. boost::optional::consume) as implementation:

```
template
void consume(F f) [&,const&,&&]
{
if (this->has_value())
f(get());
}
```

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.