C++ Logo

std-proposals

Advanced search

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

From: Sebastian Wittmeier <wittmeier_at_[hidden]>
Date: Fri, 18 Sep 2026 14:15:41 +0200
Correct me, if I am wrong, but I think it is legal C++ (no ODR violation), if different translation units declare a function with different default arguments?   The rule is probably (currently) more strict for the template type. (And how about default template arguments?)     The default function arguments are kind of "after the declaration". How to interpret the call. Perhaps it can be done similarly for the derived template type.   On the other hand default function arguments lead to various problems. And perhaps would be done differently today. Those should be considered, before extending their functionality.     -----Ursprüngliche Nachricht----- Von:shyeyian via Std-Proposals <std-proposals_at_[hidden]> Gesendet:Fr 18.09.2026 09:04 Betreff:[std-proposals] Should a default argument provide the default template type? An:std-proposals_at_[hidden]; CC:shyeyian_at_[hidden]; 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 12:22:37