Date: Thu, 16 Feb 2023 19:21:10 +0100
On 2/16/23 18:23, Jason McKesson via Std-Proposals wrote:
> ```
> class foo
> {
> private:
> std::no_rentry nr_;
>
> public:
>
> int no_reenter() {std::lock_rentry lock(nr_); if(!lock) return -1;
> /*do stuff*/}
> };
> ```
>
> So on the one hand, we have something that, while useful, doesn't seem
> *deserving* of a language solution. And on the other hand, we have
> something for which a library solution appears adequate. It may be
> slightly inelegant, but it works.
And can be further evolved as
```
int no_reenter() { return call_no_rentry(nr_, -1, do_stuff); }
```
where "do_stuff" could be a lambda expression or a private member
function.
The call_no_rentry() function could look something like this:
```
template <typename T, typename F, typename... Args>
T call_no_rentry(std::no_rentry& guard, T error, F&& fn, Args&&...
args) {
std::lock_retry lock(guard);
if (!lock) return error;
return std::invoke(fn, std::forward<Args>(args)...);
}
```
Disclaimer: The above code snippets are untested.
> ```
> class foo
> {
> private:
> std::no_rentry nr_;
>
> public:
>
> int no_reenter() {std::lock_rentry lock(nr_); if(!lock) return -1;
> /*do stuff*/}
> };
> ```
>
> So on the one hand, we have something that, while useful, doesn't seem
> *deserving* of a language solution. And on the other hand, we have
> something for which a library solution appears adequate. It may be
> slightly inelegant, but it works.
And can be further evolved as
```
int no_reenter() { return call_no_rentry(nr_, -1, do_stuff); }
```
where "do_stuff" could be a lambda expression or a private member
function.
The call_no_rentry() function could look something like this:
```
template <typename T, typename F, typename... Args>
T call_no_rentry(std::no_rentry& guard, T error, F&& fn, Args&&...
args) {
std::lock_retry lock(guard);
if (!lock) return error;
return std::invoke(fn, std::forward<Args>(args)...);
}
```
Disclaimer: The above code snippets are untested.
Received on 2023-02-16 18:21:13