C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Default parameters should work with auto

From: 叶易安 <shyeyian_at_[hidden]>
Date: Mon, 23 Mar 2026 00:06:29 +0800
> 2026.03.22 16:25,Ivan Lazaric via Std-Proposals <std-proposals_at_[hidden]> wrote:
>
> Would it be possible/make sense/be better to default the template parameter to decltype of expression when possible?
> void func(auto arg = 0) { }
>
> // equivalent to:
>
> template<typename Arg = decltype(0)>
> void func(Arg arg = 0) { }

Exactly! Your reformulation makes this much clearer.

> 2026.03.22 17:17,Bo Persson via Std-Proposals <std-proposals_at_[hidden]> wrote:
>
> It would be less typing to just add an overload:
>
> void func() { func(0); }

While this does work under the current standard, I'd like to point out two issues:

   1. An overload is different from a default argument - overload resolution now participates.

   2. With multiple parameters, it will become verbose. Consider:

     - void setblock(std::integral auto x = 0, std::integral auto y = 0, std::intergal auto z = 0, auto block = grass_block{})`

    now you need to write:

      - setblock(integral auto x, integral auto y, integral auto z, auto block) { ... }
      - setblock(integral auto x, integral auto y, integral auto z) { return setblock(x, y, z, grass_block{}); }
      - setblock(integral auto x, integral auto y) { return setblock(x, y, 0, grass_block{}); }
      - setblock(integral auto x) { return setblock(x, 0, 0, grass_block{}); }
      - setblock() { return setblock(0, 0, 0, grass_block{}); }

    which seems to be not so graceful...?


> On Sunday, March 22nd, 2026 at 10:25 AM, Ivan Lazaric via Std-Proposals
> <std-proposals_at_[hidden]> wrote:
>
>> Would it be possible/make sense/be better to default the template
>> parameter to decltype of expression when possible?
>>
>> void func(auto arg = 0) { }
>>
>> // equivalent to:
>>
>> template<typename Arg = decltype(0)>
>> void func(Arg arg = 0) { }
>
> That's a nice way to specify it. It works for cases where the type of
> the default argument depends on other template parameters, and even
> cases where the arguments aren't being type-deduced, like:
>
> void func(auto arg = 0);
> auto p = func<>;
>
> You just need some wording to allow the previous function parameters to
> appear in (an unevaluated context of) the default argument, so you can
> write:
>
> void func(auto x, auto y = decltype(x){});
>
> even though `decltype(x){}` can't technically appear in the template head.

Yes, this is indeed a clean way to specify the semantics. As you noted, it also handles cases where the default type depends on other template parameters gracefully. I'm in favor of this direction.

Received on 2026-03-22 16:06:48