Reputation:
I am learning C++ templates using the resource listed here. In particular, read about template argument deduction. Now, after reading, to further clear up my concept of the topic I tried the following example that compiles in gcc but not in clang and msvc. Demo
template<typename T = int> void f()
{
}
template<typename T> void func(T)
{
}
int main()
{
func(f); //works in gcc but not in clang and msvc
func(f<>); //works in all
}
As we can see, the above example compiles fine in gcc but not in clang and msvc. My question is which compiler is right here according to the latest standard?
Upvotes: 2
Views: 140
Reputation: 1
This is CWG 2608 and the program is well-formed so that gcc is correct in accepting the program.
From temp.arg.explicit#4 which was added due to cwg 2608, the default template argument can be used and the empty template argument list <>
can be omitted.
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.
(emphasis mine)
Note the bold highlighted part in the above quoted statement, which means that the empty template argument list <>
can be omitted in your example because all of the template arguments can be obtained from default template-arguments.
Further, over.over#3 can also be used here to see that the specialization generated from temp.arg.explicit is added to the overloaded set:
The specialization, if any, generated by template argument deduction ([temp.over], [temp.deduct.funcaddr], [temp.arg.explicit]) for each function template named is added to the set of selected functions considered.
(emphasis mine)
This means that at the end, the call func(f);
is well-formed and uses the generated specialization f<int>
.
Upvotes: 2