Date: Tue, 07 Jun 2022 23:06:42 +0200
This issue was discussed recently on StackOverflow. With Anoop's proposed
revised wording for C++20, all three of the following should be valid:
template<typename T = int> void func() { }
int main()
{
func(); //#1
int main()
{
func(); //#1
auto a = func<>; //#2
auto b = func; //#3
}
}
However, most current compilers accept #1 and #2 but reject #3. It may be
appropriate to extend the example in the specification to include these three
cases to make clear which of them are intended to be valid.
Note that the draft wording for C++23 (13.10.2 in N4910) has been simplified
compared with the C++20 standard, but suffers from the same issue.
On 7 Jun 2022 at 23:15, Anoop Rana via Std-Proposals wrote:
I was reading
temp.arg.explicit
and noticed that there may be some defect in the
wording there. In particular, it currently says:
"
Trailing template arguments that can be
deduced
or obtained from default
template-argument
s may be omitted from the list of explicit
template-argument
s
.
A trailing template parameter pack (
[temp.variadic]) not otherwise deduced will be
deduced as an empty sequence of template arguments
.
If all of the template arguments can be deduced
, they may all be omitted; in this
case, the empty template argument list <> itself may also be omitted
.
"
(emphasis mine)
Note carefully, in the last statement it says that when all of the template
arguments can be deduced then they may all be omitted and in this case the
empty <> itself may also be omitted. This is an issue because it does not
mention that we can also omit the empty <> when all of the template arguments
can be obtained from the default template arguments. Since deducing arguments
and using default values are two different things, there should be a separate
mention of the latter.
Only in the first sentence(quoted above) there is a mention of default template
arguments but that is only in the context of the list of explicit template arguments.
This means according to the current wording(sentence 3 quoted above) the
following should be invalid:
template<typename T = int>
void func()
{
}
int main()
{
func(); //this should be invalid according to the current wording
}
void func()
{
}
int main()
{
func(); //this should be invalid according to the current wording
}
But we know that the above is not invalid and so there should be changes made
to the sentence 3 quoted above to include the case of default template
arguments.
Resolution:
The simplest resolution is to change the 3rd sentence quoted above to the
following:
If all of the template arguments can be deduced
or obtained from default template
arguments, they may all be omitted; in this case, the empty template argument list
<> itself may also be omitted
.
(end resolution)
I have highlighted the addition/change that i made in the above sentence to make
it more clear/easily visible.
Received on 2022-06-07 21:06:47