C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Forwarding refs for class template arg deduction

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Sun, 24 Mar 2024 14:41:34 -0400
On Sun, Mar 24, 2024 at 2:23 PM Frederick Virchanza Gotham via
Std-Proposals <std-proposals_at_[hidden]> wrote:
>
> Up until C++17, we had to use 'lock_guard' as follows:
>
> lock_guard<mutex> mylock(mymutex);
>
> But then C++17 came, and we could do:
>
> lock_guard mylock(mymutex);
>
> There's one weakness here though: If we want the constructor's parameters to be forwarding references, then we cannot do the following:
>
> template<typename T>
> class lock_guard {
> public:
>
> template<typename T>
> lock_guard(T &&arg)
> {
>
> }
> };

Use an explicit deduction guide:

```
template<typename T>
lock_guard(T &&arg) -> lock_guard<std::remove_cvref_t<T>>;
```

Received on 2024-03-24 18:41:45