I was using parameter pack when I noticed that one such case(shown below) compiles fine in gcc and clang but not in msvc:
template<class T> void func(T a, T b= T{})
{
}
template<class T, class... S> void func(T a, S... b)
{
}
int main()
{
func(1); // Should this call succeed?
}
Here is the link for verifying the same: https://godbolt.org/z/8KsrcnMez
As can be seen the above program fails in msvc with the error message:
<source>(13): error C2668: 'func': ambiguous call to overloaded function
<source>(6): note: could be 'void func<int,>(T)'
with
[
T=int
]
<source>(2): note: or 'void func<int>(T,T)'
with
[
T=int
]
<source>(13): note: while trying to match the argument list '(int)'
But the same compiles fine with gcc and clang.
Which compiler(s) is right here?
MSVC is right in rejecting the code. According to temp.func.order#5.example-2 the call
func(1)
is ambiguous. The example given in the standard is as follows:This means that in the example in question, the call
func(1)
is ambiguous as well.Errata
Note that cppreference has errata in below given example:
As can be seen in the above example, cpprefernce incorrectly says that the first overload
#1
should be choosen.This has been fixed in recent edit that i made.