On Tue, 29 Mar 2022 at 16:38, Tom Honermann via Std-Proposals <std-proposals@lists.isocpp.org> 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 / 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.