This is a compile time error for compare1
I understand the reason.
But why doesn’t it fail for compare2
?
#include <iostream>
#include <vector>
#include <string>
using namespace std;
template<typename T1, typename T2>
const T1& compare1(const T1& a, const T2& b) //Can be error: if a and b of different array sizes.
{
return a < b ? a : b;
}
template<typename T1, typename T2>
bool compare2(const T1& a, const T2& b) //Why is differenent array size error not applicable here?
{
return a < b ? a : b;
}
int main()
{
compare1("hello", "world"); //sucess
compare1("hello", "worldx"); //fail as expected
compare2("hello", "worldx"); // passed which is not expected.
}