I am surprised that this code doesn't compile:
#include <type_traits>
template <int number, bool isTest1>
class TestClass1
{
};
template <int number, bool isTest1>
class TestClass2
{
};
template <int number, bool isTest1>
using MyTestClass = typename std::conditional<true, TestClass1<number, isTest1>, TestClass2<number, isTest1>>::type;
template <int number, bool isTest1>
auto createTest() -> MyTestClass<number, isTest1>
{
return MyTestClass<number, isTest1>();
}
template <int number, bool isTest1>
auto function(int something, const MyTestClass<number, isTest1>& test) -> int
{
return number;
}
int main()
{
const MyTestClass<2, true> test = createTest<2, true>();
function(123, test);
// This works:
// function<2, true>(123, test);
return 0;
}
The error message is:
main.cpp:31:23: error: no matching function for call to 'function(int, MyTestClass<2, true>&)'
31 | function(123, test);
| ^
main.cpp:23:6: note: candidate: 'template<int number, bool isTest1> int function(int, MyTestClass<number, isTest1>&)'
23 | auto function(int something, const MyTestClass<number, isTest1>& test) -> int
| ^~~~~~~~
main.cpp:23:6: note: template argument deduction/substitution failed:
main.cpp:31:23: note: couldn't deduce template parameter 'number'
31 | function(123, test);
| ^
See in Compiler Explorer: https://godbolt.org/z/71avfordM
I am surprised because everything seems to be there for template parameter deduction. I can spot no ambiguity or whatsoever. Does the compiler have a good reason to refuse my code?