In the following code why does the explicit expansion of template function foo fail to compile, yet the expansion of bar compiles successfully ? Live link - https://godbolt.org/z/o8Ea49KEb
template <typename T1, typename T2>
T1 foo(T2) { T2(42); return T1{}; };
template <typename T1, typename T2>
T1 bar(void) { T2(42); return T1{}; };
int main()
{
foo<int, void>(); // fails
bar<int, void>(); // works
}
Note that the template parameter T2 is used in the body of both functions and the only difference is that the function parameter to bar has been manually substituted.
This question was inspired after reading std::conditional - Invalid parameter type ‘void’ even when testing for 'void' and trying to simplify the issue.