C++ Logo

std-proposals

Advanced search

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

From: Timur Doumler <cpp_at_[hidden]>
Date: Tue, 29 Mar 2022 11:55:19 +0100
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 = []{});

so that if someone doesn’t want to pass the optional two callbacks, they can just leave them out. And to let the function template deduce the type of all three.

In practice the signature looked a bit more complicated as there are constraints on the function signature of each callback, but the idea is the same.

Cheers,
Timur

> On 29 Mar 2022, at 01:39, Jason McKesson via Std-Proposals <std-proposals_at_[hidden]> wrote:
>
> On Mon, Mar 28, 2022 at 3:07 PM Kilian Henneberger via Std-Proposals
> <std-proposals_at_[hidden] <mailto:std-proposals_at_[hidden]>> wrote:
>>
>> Hello,
>>
>> I am wondering whether default arguments should participate in template
>> argument deduction.
>> Currently they don't. Therefore, given following function templates:
>>
>> template<class Pred>
>> void sort(Pred p = std::less{});
>>
>> void set_callback(auto cb = []{});
>>
>> auto create_logger(auto sl = std::source_location::current());
>>
>> Calling any of those without providing function arguments, will fail to
>> compile.
>
> Your first example seems like the kind of code someone would
> reasonably write. But your second and third examples are so bizarre
> that making them errors makes a lot of sense.
>
> Exactly what type `cb` in the second example should even be is
> unclear. Putting lambdas in function declarations is pretty dubious,
> and even moreso for template functions that generate a new function
> (and presumably a new type) for different instantiations. If you had
> other template parameters there, you could start getting weird
> behavior.
>
> And your last example is a perfect reason *why* it should fail to
> compile. The person who wrote that function clearly thinks that the
> `auto` here ought to work like `auto` when declaring a variable. And
> it doesn't. They do not intend for the function to be a template, but
> that's what they wrote. And they need to *know* that they wrote their
> code incorrectly.
>
> We should not play into peoples' misconceptions about how language
> features work.
>
>> Yes, there are solutions to make this work already today without any
>> changes to the standard.
>> Therefore the only motivation that I can give is, that this would make
>> programming more convenient.
>> In particular, together with abbreviated function templates.
>>
>> I would very much welcome your feedback!
>> Thanks and kind regards,
>> Kilian
>> --
>> Std-Proposals mailing list
>> Std-Proposals_at_[hidden]
>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden] <mailto:Std-Proposals_at_[hidden]>
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals <https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals>

Received on 2022-03-29 10:55:23