C++ Logo

std-proposals

Advanced search

Re: [std-proposals] long return lambda

From: Pavel Vazharov <freakpv_at_[hidden]>
Date: Tue, 11 Apr 2023 16:39:39 +0300
> auto BAIL = [p](void) -> long return bool { strcpy(p,
"0000000000000000"); return false; };

Or you can use something like std::scope_success/failed:

```
template <typename Func>

class scope_guard

{

    Func func_;

    bool active_;



public:

    explicit scope_guard(Func&& func)
    : func_(std::move(func))

    , active_(true)

    {

    }

    ~scope_guard() { if (active_) func_(); }



    void dismiss() { active_ = false; }

};

bool Serial(char *const p)
{
    scope_guard on_error([&]{ strcpy(p, "0000000000000000"); });

    if (<error>) return false;

    if (<another-error>) return false;

    on_error.dismiss();
    return true;
}
```

Received on 2023-04-11 13:39:52