Date: Sun, 22 Mar 2026 13:11:14 +0800
Hi, std-proposals mailing list. I'm a C++ learner, and i'm confused in a C++ grammar related question below. Could you please help and give some of your advice? Thanks!
In this code,
===== tmp.cpp =====
int func(auto arg = 42) { return int(arg); }
int value = func(); // Expected: with default argument = 42, and `auto` deduced into `int`.
=====
We expect that the default argument is 42, and `auto` is **deduced** into `int`.
But actually, in the current C++ standard, this `auto` can be deduced by user-provided arguments, but **cannot** be deduced by default-provided arguments. C++ complains about we cannot choose `int` as the default type. Let's see the outputs.
===== C++ draft =====
https://eel.is/c++draft/dcl.fct.default, which does not mentioned this case.
=====
===== cppreference =====
https://cppreference.com/w/cpp/language/default_arguments.html, which does not mentioned this case.
=====
===== g++ (version=15) =====
shyeyian_at_macbook tmp % g++-15 -std=c++26 tmp.cpp -o tmp
tmp.cpp:2:17: error: no matching function for call to 'func()'
2 | int value = func();
| ~~~~^~
tmp.cpp:2:17: note: there is 1 candidate
tmp.cpp:1:5: note: candidate 1: 'template<class auto:1> int func(auto:1)'
1 | int func(auto arg = 42) { return arg * 2; }
| ^~~~
tmp.cpp:1:5: note: template argument deduction/substitution failed:
tmp.cpp:2:17: note: couldn't deduce template parameter 'auto:1'
2 | int value = func();
| ~~~~^~
=====
===== clang++ (version=22) =====
shyeyian_at_macbook tmp % clang++ -std=c++26 tmp.cpp -o tmp
tmp.cpp:2:13: error: no matching function for call to 'func'
2 | int value = func();
| ^~~~
tmp.cpp:1:5: note: candidate template ignored: couldn't infer template argument
'arg:auto'
1 | int func(auto arg = 42) { return arg * 2; }
| ^
1 error generated.
=====
Because the default argument is a value, it always has a type, and then this `auto` here is always deducable. So that, In my perspective, this default argument should work with default type as `int`.
Thank you for any feedbacks!
In this code,
===== tmp.cpp =====
int func(auto arg = 42) { return int(arg); }
int value = func(); // Expected: with default argument = 42, and `auto` deduced into `int`.
=====
We expect that the default argument is 42, and `auto` is **deduced** into `int`.
But actually, in the current C++ standard, this `auto` can be deduced by user-provided arguments, but **cannot** be deduced by default-provided arguments. C++ complains about we cannot choose `int` as the default type. Let's see the outputs.
===== C++ draft =====
https://eel.is/c++draft/dcl.fct.default, which does not mentioned this case.
=====
===== cppreference =====
https://cppreference.com/w/cpp/language/default_arguments.html, which does not mentioned this case.
=====
===== g++ (version=15) =====
shyeyian_at_macbook tmp % g++-15 -std=c++26 tmp.cpp -o tmp
tmp.cpp:2:17: error: no matching function for call to 'func()'
2 | int value = func();
| ~~~~^~
tmp.cpp:2:17: note: there is 1 candidate
tmp.cpp:1:5: note: candidate 1: 'template<class auto:1> int func(auto:1)'
1 | int func(auto arg = 42) { return arg * 2; }
| ^~~~
tmp.cpp:1:5: note: template argument deduction/substitution failed:
tmp.cpp:2:17: note: couldn't deduce template parameter 'auto:1'
2 | int value = func();
| ~~~~^~
=====
===== clang++ (version=22) =====
shyeyian_at_macbook tmp % clang++ -std=c++26 tmp.cpp -o tmp
tmp.cpp:2:13: error: no matching function for call to 'func'
2 | int value = func();
| ^~~~
tmp.cpp:1:5: note: candidate template ignored: couldn't infer template argument
'arg:auto'
1 | int func(auto arg = 42) { return arg * 2; }
| ^
1 error generated.
=====
Because the default argument is a value, it always has a type, and then this `auto` here is always deducable. So that, In my perspective, this default argument should work with default type as `int`.
Thank you for any feedbacks!
Received on 2026-03-22 05:11:36
