Skip to content

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

@mknaleczb

Description

@mknaleczb

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions