C++ Logo

std-proposals

Advanced search

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

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Mon, 28 Mar 2022 20:39:58 -0400
On Mon, Mar 28, 2022 at 3:07 PM Kilian Henneberger via Std-Proposals
<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

Received on 2022-03-29 00:40:16