This example is from C++ templates book by Josuttis :
#include <iostream>
#include <cstring>
#include <string>
// maximum of two values of any type (call-by-reference)
template <typename T>
inline T const& max (T const& a, T const& b)
{
return a < b ? b : a;
}
// maximum of two C-strings (call-by-value)
inline char const* max (char const* a, char const* b)
{
return std::strcmp(a,b) < 0 ? b : a;
}
// maximum of three values of any type (call-by-reference)
template <typename T>
inline T const& max (T const& a, T const& b, T const& c)
{
return max (max(a,b), c); // error, if max(a,b) uses call-by-value
}
int main ()
{
::max(7, 42, 68); // OK
const char* s1 = "frederic";
const char* s2 = "anica";
const char* s3 = "lucas";
::max(s1, s2, s3); // ERROR
}
He says that the reason for error in ::max(s1, s2, s3) is that for C-strings max(max(a,b),c) calls max(a,b) that creates a new temporary local value that may be returned by the function by reference.
I am not getting how a new local value is getting created ?
For C-strings, this code creates a local value, i.e, a local variable that stores an address (pointer of type char const *):
std::strcmp(a,b) < 0 ? b : a;
Hence returning a reference to this (using the template functions) leads to an error. In this case the reference of type
char const * const &to a local is returned by the template functionmaxafter the C-stringmaxhas returned a copy. The template functions have to return pointers by value instead of reference.The template functions need to be overloaded for pointer types.