Home > Net >  How to return an std::optional lambda?
How to return an std::optional lambda?

Time:03-08

How can I declare a function that returns an std::optional lambda? e.g.

<what_do_i_put_here?> foo(bool b) {
    if(b) return std::nullopt;
    return [](int) { ... };
}

CodePudding user response:

How about using the ternary operator? It will automatically deduce the correct optional type

#include <optional>
auto foo(bool b) {
  return b ? std::nullopt : std::optional{[](int){}};
}

CodePudding user response:

You can add a level of indirection to deduce the type via auto and decltype:

#include <optional>

auto foo_impl(){
    return [](int){};
}

std::optional<decltype(foo_impl())> foo(bool b) {
    if(b) return std::nullopt;
    return foo_impl();
}

CodePudding user response:

You can do it something like the following

#include <iostream>
#include <optional>

auto f( bool b )
{
    auto l = [] { std::cout << "Hello World!";  };
    std::optional<decltype( l )> opt;

    if (b)
    {
        // some code
        opt = l;
    }
    else
    {
        // some other cod
    }

    return opt;
}

int main()
{
    ( *f( true  ) )( );
}

Another way is to use std::function with std::optional as for example

std::optional<std::function<void( int )>> g( bool b )
{
    if (b)
    {
        return std::function<void( int )>( []( int x ) { std::cout << x;  } );
    }
    else
    {
        return std::function<void( int )>( []( int x ) { std::cout << 2 * x; } );
    }
}

CodePudding user response:

In the upcoming C 23 you can avoid an extra function while only constructing your lambda in case it is needed. Unfortunately, support for this is currently experimental:

#include <optional>

auto foo(bool const b) {
    return (b ? std::optional<bool>(true) : std::optional<bool>())
      .transform([](bool){
          return [](int){ return 42;};
      });   
}

(demo with gcc trunk)

This uses the new std::optional<T>::transform(F) member function.

  • Related