I started reading C ++ Templates - the complete guide to Josuttis and Vandevoorde. And my tiny mind is stuck.
The authors state that "you must limit your changes to the number of parameters or explicitly specify the template parameters", using this as an example that causes problems:
template <typename T>
inline T const& max (T const& a, T const& b)
{
return a < b ? b : a;
}
inline char const* const& max( char const* a, char const* b)
{
return std::strcmp(a, b) < 0 ? b : a;
}
template <typename T>
inline T const& max (T const& a, T const& b, T const& c)
{
return max( max(a,b), c);
}
It is said that using the 3 argument max version is a mistake -
const char * s1 = "fred";
const char * s2 = "anica";
const char * s3 = "lucas";
::max(s1, s2, s3);
"because it max(a,b)creates a new temporary local value for C-lines , which can be returned by function by reference.". But I compiled and had a great time.
Can someone explain what the author says in this example?
source
share