I have a question about C++ templates. The following codes segfaults.
template <typename T1, typename T2>
inline T1 const& max(T1 const &a, T2 const &b) {
return a < b ? b : a;
}
int main() {
std::cout << max(4.9, 4) << std::endl;
}
However, remove the & and it does the right thing.
template<typename T1, typename T2>
inline T1 const max(T1 const &a, T2 const &b) {
return a < b ? b : a;
}
int main() {
std::cout << max(4.9, 4) << std::endl;
}
Furthermore, just use T instead of T1 and T2 and it works just fine.
template<typename T>
inline T const& max(T const &a, T const &b) {
return a < b ? b : a;
}
int main() {
std::cout << max(4, 5) << std::endl;
}
What am I doing wrong here ?