-
Notifications
You must be signed in to change notification settings - Fork 70
Open
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<std::string> 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 <typename F>
void consume(F f) [&,const&,&&]
{
if (this->has_value())
f(get());
}
Metadata
Metadata
Assignees
Labels
No labels