C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Should a default argument provide the default template type?

From: Jan Schultke <janschultke_at_[hidden]>
Date: Fri, 18 Sep 2026 09:15:27 +0200
Can you come up with an example where this feature would be genuinely
useful?

In your example with func(auto&& arg = 42), it's completely pointless for
the function to be a template. It should be a plain function taking int&&,
so that's really meant to be static type inference rather than an
abbreviated function template.

No one would write template<class T = decltype(42)> either, they would just
specify int there.

The current design forces you to make a conscious decision about whether to
match the type T (by having a default argument of T(42)) or by providing a
type for T and then letting the implicit conversion from int to T do its
thing. I consider it beneficial to the language that such a decision must
be made instead of blindly taking the type of the default argument.

On Fri, 18 Sept 2026 at 09:04, shyeyian via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> Consider this declaration:
>
> ```cpp
> void func(auto&& arg = 42);
> int main()
> {
> func();
> }
> ```
>
> Both gcc and clang reject the call:
>
> ```text
> [shyeyian_at_core cppmake]$ g++ -std=c++26 test/main.cpp
> test/main.cpp: In function 'int main()':
> test/main.cpp:5:9: error: no matching function for call to 'func()'
> 5 | func();
> | ~~~~^~
> * there is 1 candidate
> * candidate 1: 'template<class auto:1> void func(auto:1&&)'
> test/main.cpp:1:6:
> 1 | void func(auto&& arg = 1);
> | ^~~~
> * template argument deduction/substitution failed:
> * couldn't deduce template parameter 'auto:1'
> test/main.cpp:5:9:
> 5 | func();
> | ~~~~^~
> [shyeyian_at_core cppmake]$ clang++ -std=c++26 test/main.cpp
> test/main.cpp:5:5: error: no matching function for call to 'func'
> 5 | func();
> | ^~~~
> test/main.cpp:1:6: note: candidate template ignored: couldn't infer
> template argument 'arg:auto'
> 1 | void func(auto&& arg = 1);
> | ^
> 1 error generated.
> [shyeyian_at_core cppmake]$
> ```
>
> This follows from the current rules: the declaration is an abbreviated
> function template. In other words, it is effectively equivalent to:
>
> ```cpp
> template<class T>
> void func(T&& arg = 42);
> ```
>
> But the declaration already contains a default expression with a known
> type! I hope the declaration to be equivalent to
>
> ```cpp
> template<class T = decltype(42)>
> void func(T&& arg = 42);
> ```
>
> This would make `func()` well-formed without changing calls that supply an
> argument. In those calls, ordinary template argument deduction would still
> determine `T`; the default template argument would be used only when
> deduction does not provide one.
>
> What do you think of this behavior? Are there any compatibility concerns I
> may have overlooked?
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2026-09-18 07:15:43