C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Default arguments should participate in template argument deduction

From: Edward Catmur <ecatmur_at_[hidden]>
Date: Tue, 29 Mar 2022 17:42:03 +0100
On Tue, 29 Mar 2022 at 16:38, Tom Honermann via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> On 3/29/22 6:55 AM, Timur Doumler via Std-Proposals wrote:
>
> I don’t agree that the second example is bizarre. In fact, I had this
> exact case in a real-world codebase not too long ago. The idea was that you
> start an audio device and pass it a process callback (which is mandatory)
> as well as “device started” and device stopped” callbacks, which are
> optional callbacks that the device will call before it starts processing
> and after it stopped processing so you can allocate/deallocate resources
> required for processing. It would be very nice to be able to write
>
> void start_device(auto process_callback, auto started_callback = []{},
> auto stopped_callback = []{});
>
> Do you really want templates in that case? It seems to me that
> std::function_ref as proposed in P0792 <https://wg21.link/p0792> / P2472
> <https://wg21.link/p2472> provides a better solution here.
>
std::function_ref is a great solution in many cases, but it does mean
paying the overhead of type erasure, so you may prefer to keep it a
template and use `std::invocable<> auto`.

It also isn't a solution if the callback needs to accept more than one set
of arguments, or an argument type which isn't easily calculated within the
signature.

Received on 2022-03-29 16:42:14